【问题标题】:What is argument type of Xamarin WebView Source?Xamarin WebView Source 的参数类型是什么?
【发布时间】:2021-07-22 07:38:12
【问题描述】:

我有一个在 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 上的 VS 2019 16.9.4 社区。Xamarin 16.9

【问题讨论】:

    标签: xaml xamarin.forms webview


    【解决方案1】:

    根据WebView.Source Property docs WebView.SourceWebViewSource 类型,所以你应该输入Uristring 而不是WebViewSource

        <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>
    

    【讨论】:

    • 我的 XAML 编辑器(参见上面的版本)没有 WebViewSource 的 x;TypeArgument。它有 HtmlWebViewSource 和 UrlWebViewSource,这两个都给我一个错误 System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type 'System.String' to type 'Xamarin.Forms.HtmlWebViewSource'。 Source=Xamarin.Forms.Core in InitializeComponent in the xaml.g.cs 代码。
    • 如果您的意思是 intelisense 不建议这样做,即使没有建议,它也可能会正常工作,是否尝试过我的解决方案?如果不工作你可以试试UrlWebViewSource
    • 是的,我确实是这个意思,是的,您的解决方案确实有效。谢谢你。另外:对于将来阅读本文的任何人,上面的 UWP Value= 是不正确的;应该只是 Value="ms-appx-web:///Getting Started Text.html" (路径中没有 UWP_asset)。
    • 您可以编辑您的问题以更正它(uwp 值),我将确保对我的答案执行相同操作,以确认它与 WebViewSourceUrlWebViewSource 一起使用?
    • 使用 WebViewSource。 UrlWebViewSource 给了我我在第一条评论中引用的错误。我将修改我的网址。
    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 2022-08-22
    • 2017-02-12
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    相关资源
    最近更新 更多