无法在FlipView中的WebView中加载网站

时间:2012-11-10 12:46:29

标签: c# .net xaml

我正在尝试在Windows应用商店应用中的FlipView控件中打开一个网站。这是我的代码:

protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    var template = flipView.ItemTemplate;
    var grid = (Grid)template.LoadContent();
    var webView = (WebView)grid.Children[0];

    webView.Navigate(new Uri("http://www.google.com"));
}

private void contentView_LoadCompleted_1(object sender, NavigationEventArgs e)
{
    var template = flipView.ItemTemplate;
    var grid = (Grid)template.LoadContent();
    var webView = (WebView)grid.Children[0];
    webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
}

和XAML:

  <FlipView
        x:Name="flipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        TabIndex="1"
        Grid.RowSpan="2"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

        <FlipView.ItemContainerStyle>
            <Style TargetType="FlipViewItem">
                <Setter Property="Margin" Value="0,137,0,0"/>
            </Style>
        </FlipView.ItemContainerStyle>

        <FlipView.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="Output">
                            <WebView x:Name="WebView1" Visibility="Collapsed" LoadCompleted="contentView_LoadCompleted_1"/>
                            <Rectangle x:Name="BlockingRect"/>
                </Grid>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

也许之前有人遇到过这个问题?解决方案是什么?

2 个答案:

答案 0 :(得分:3)

callisto WebViewExtension https://github.com/timheuer/callisto/wiki/WebViewExtension是我发现将WebView内容绑定到FlipView的最佳方式。

在默认用法中,WebView项目中的某些触摸(特定于tap)交互仍然存在问题,因为FlipView正在处理这些交互,而不是让它们落入WebView。因此,导致WebView内的链接无法与之交互。当使用鼠标时,这不是问题,因为点击会进入WebView。

答案 1 :(得分:0)

首先将Uniq名称提供给您的FrameworkControlllıkex:Name="ItemDetailPageWebView"    比复制和过去的代码背后的什么ı赖特下面

XAML:

    <FlipView x:Name="flipView"
       AutomationProperties.AutomationId="ItemsFlipView"
       AutomationProperties.Name="Item Details"
       TabIndex="1" Grid.Row="1"
       ItemsSource="{Binding Source={StaticResource itemsViewSource}}"   
       Loaded="ItemDetailPageflipView_Loaded">
              <FlipView.ItemTemplate>
                  <DataTemplate>
                     <Grid>       
                        <ScrollViewer Margin="0,0,0,0" Style="{StaticResource HorizontalScrollViewerStyle}" >
                            <StackPanel  Orientation="Horizontal" Height="628" Width="2755">
                                  <WebView x:Name="ItemDetailPageWebView" Height="443" Margin="53,82,0,0" VerticalAlignment="Top" Width="435" />
                            </StackPanel>
                      </ScrollViewer>
                 </Grid>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

C#:

//-----------------------------------------------------------------------------------
            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;
            }

            //Here FlipView Loaded Event 
            private void ItemDetailPageflipView_Loaded(object sender, RoutedEventArgs e)
            {
                WebView ItemDetailPageWebView = FindVisualChildByName<WebView>(this.flipView, "ItemDetailPageWebView");
                ItemDetailPageWebView.Navigate(new Uri("http://www.google.com"));
            }

        //-----------------------------------------------------------------------------------

如果我帮助并点击答案,请给我一个+。快乐的编码;)