ListView项目在触摸时消失或单击自定义webview

时间:2017-09-06 19:36:25

标签: listview webview xamarin.forms xamarin.uwp custom-renderer

我正在通过Xamarin PCL创建一个UWP应用程序。在我的页面中,我在另一个堆栈布局中有一个listview和一个本机uwp webview(通过自定义渲染器渲染)。

我的问题是: - 当我在具有触摸屏的Windows笔记本电脑中运行应用程序时,当前显示在屏幕上的列表项目在点击或触摸webview容器上的任何位置时消失。点击或触摸任何其他位置都不会影响列表。

但是,列表项目列表中存在列表项目,因为单击任何项​​目单元格,应用程序正在按预期工作。在模拟器应用程序正常工作。

我已检查过长列表数据,在这种情况下,只有当前正在查看的项目正在消失而其他项目正确显示,因为我可以在向下滚动时看到它们。

请建议任何解决方案。

查看

public class NativeWebView : View
{
    public NativeWebView()
    {
        var htmlSource = new HtmlWebViewSource();
    }

    public static readonly BindableProperty UriProperty = BindableProperty.Create(
       propertyName: "Uri",
       returnType: typeof(string),
       declaringType: typeof(NativeWebView),
       defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

CustomRenderer for webview

[assembly: ExportRenderer(typeof(NativeWebView), typeof(CustomWebViewRenderer))]

命名空间XYZ

{

public class CustomWebViewRenderer : ViewRenderer<NativeWebView, WebView>

{

    static WebView webView;

    public CustomWebViewRenderer()
    {            
    }

    protected override void OnElementChanged(ElementChangedEventArgs<NativeWebView> e)
    {

        base.OnElementChanged(e);

        if (Control == null)
        {
            SetNativeControl(new WebView());
            webView = Control;
        }

        if (e.NewElement != null)
        {
            Control.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", Element.Uri));
        }
    }

}

Xaml.cs

public ClassName()
    {
        InitializeComponent();

        NativeWebView nativeWebView = new NativeWebView();
        nativeWebView.Uri = "index.html";
        nativeWebView.HorizontalOptions = LayoutOptions.FillAndExpand;
        nativeWebView.VerticalOptions = LayoutOptions.FillAndExpand;

        myStackLayout.Children.Add(nativeWebView);

        this.viewModel = new PersonViewModel();

        this.BindingContext = this.viewModel;

        this.LoadItems(null, EventArgs.Empty);
    }

    public void LoadItems(object sender, EventArgs e)
    {
        this.listView.ItemsSource = this.viewModel.PersonItems;
    }

的Xaml

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <StackLayout Grid.Column="0" Orientation="Vertical">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="40" />
                </Grid.RowDefinitions>
            <StackLayout Grid.Row="0" Orientation="Vertical">
                <ListView x:Name="listView" SeparatorVisibility="None" RowHeight="80">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Vertical" Padding="2,15">
                                    <Label Text ="{Binding FirstName, Mode=OneWay}" HorizontalTextAlignment="Start" TextColor="#FFFFFF" Margin="20, 0, 0, 0"></Label>
                                    <Label Text ="{Binding LastName, Mode=OneWay}" HorizontalTextAlignment="Start" TextColor="#FFFFFF" Margin="20, 0, 0, 10"></Label>
                                    <StackLayout HeightRequest="1" WidthRequest="190" HorizontalOptions="Start"  BackgroundColor="White" Margin="20, 0, 0, 10" />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </Grid>
        </StackLayout>

        <StackLayout x:Name="myStackLayout" Grid.Row="0" Grid.Column="1" Spacing="0"  >
        </StackLayout>
    </Grid>

0 个答案:

没有答案