Xamarin WebView Source 的参数类型是什么?

问题描述

我有一个在 XAML 中完全定义的 WebView。它显示来自本地文件的“帮助文件”。它适用于带有 Source="file:///android_asset/Getting Started Text.html" 的 Android。我现在也试图让它在 UWP 上工作,所以我添加OnPlatform,如下所示。我得到:

XFC0009 找不到“源”的属性、BindableProperty 或事件,或者值和属性间的类型不匹配。在 上。

我也试过 x:TypeArguments="x:String" 并得到同样的错误x:TypeArguments 应该用于 Webview.source 吗?

            <WebView  WidthRequest="1000" HeightRequest="1000">
                <WebView.source>
                    <OnPlatform x:TypeArguments="x:Uri">
                        <OnPlatform.Platforms>
                            <On Platform="Android" Value="file:///android_asset/Getting Started Text.html" />
                            <On Platform="UWP" Value="ms-appx-web:///Getting Started Text.html" />
                        </OnPlatform.Platforms>
                    </OnPlatform>
                </WebView.source>
            </WebView>

Windows 10 Pro 64.Xamarin 16.9 上的 VS 2019 16.9.4 社区

解决方法

根据 WebView.Source Property docs WebView.SourceWebViewSource 类型,因此您应该输入 Uri 而不是 stringWebViewSource

    <WebView WidthRequest="1000"
             HeightRequest="1000">
        <WebView.Source>
            <OnPlatform x:TypeArguments="WebViewSource">
                <OnPlatform.Platforms>
                    <On Platform="Android"
                        Value="file:///android_asset/Getting Started Text.html"/>
                    <On Platform="UWP"
                        Value="ms-appx-web:///Getting Started Text.html"/>
                </OnPlatform.Platforms>
            </OnPlatform>
        </WebView.Source>
    </WebView>