2014-11-24 96 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