2014-11-24 106 views
1

對於我正在開發的應用程序,我想使用帶有三個WebView的Hub,您可以滾動瀏覽。我曾嘗試:集線器中的WebViews Windows Phone 8.1

  • 乾脆把在輪轂部web視圖(這不工作 - 需要一個DataTemplate)

  • 把一個DataTemplate內的WebView在HubSection(此編譯,但web視圖不是後面的代碼)

  • 使用以下代碼來試圖找到控制視覺,而使用上述方法可訪問:

    public static T FindVisualChildByName<T>(DependencyObject parent, string name) 
    where T : DependencyObject 
    
    { 
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
        { 
         var child = VisualTreeHelper.GetChild(parent, i); 
         string controlName = child.GetValue(Control.NameProperty) as string; 
         if (controlName == name) 
         { 
          return child as T; 
         } 
         else 
         { 
          T result = FindVisualChildByName<T>(child, name); 
          if (result != null) 
           return result; 
         } 
        } 
        return null; 
    } 
    

這沒有找到任何視覺元素。我怎麼能有一個WebViews的中心內?

回答

1

確保在Page加載後只搜索VisualTree

<Page Loaded="Page_Loaded"> 
    <Grid> 
     <Hub x:Name="myHub"> 
      <HubSection x:Name="myHubSection"> 
       <DataTemplate> 
        <WebView x:Name="myWebView"></WebView> 
       </DataTemplate> 
      </HubSection> 
     </Hub> 
    </Grid> 
</Page> 

private void Page_Loaded(object sender, RoutedEventArgs e) 
{ 
    var hub_section = FindVisualChildByName<HubSection>(this.myHub, "myHubSection"); 
    var web_view = FindVisualChildByName<WebView>(hub_section, "myWebView"); 
} 

enter image description here

+0

謝謝!標記爲答案。 – SFX 2014-12-01 19:44:25

-2

做到像Chubosaurus軟件他回答說。確保你已經加載了WebView!

但你不必搜索HubSection,你可以簡單地在函數中使用它的名字是這樣的:

WebView webview = FindChildControl<WebView>(WebViewSectionName, "MyWebView") as WebView; 
+0

它確實看起來像是對我的回答。重複,但答案。 – EJP 2014-11-27 23:51:43

+0

我想說的是,方法_SFX_如何嘗試訪問WebView是正確的,並且_Chubosaurus Software_給出的awnser也很好,但它包含了一個額外的代碼行,這是不必要的,因爲您可以通過訪問HubSection後面的代碼。您無法訪問WebView所在的DataTemplate中的控件。 – Cika012 2014-11-28 18:45:12

+0

關於第二個說明,我剛纔注意到,上述方法與我使用的方法有點不同。我發現它[這裏](http://stackoverflow.com/questions/15189715/how-to-access-a-control-inside-the-data-template-in-c-sharp-metro-ui-in -the-碼?answertab =票#製表頂部) – Cika012 2014-11-28 18:46:22