XAML WebView绑定到字符串在Xamarin Forms中不起作用 [英] XAML WebView binding to string not working in Xamarin Forms

查看:91
本文介绍了XAML WebView绑定到字符串在Xamarin Forms中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和Xamarin Forms的新手.我正在执行Webview并从API获取源URL. (对于这个问题,我已经硬编码了值).我绑定了源URL,而不是将值添加到XAML中的Source.但这不起作用.堆栈和论坛中几乎没有解决方案.我试过了.但是没有用.有人请帮我解决这个问题.

I'm new to C# and Xamarin Forms. I'm having a webview and getting source url from an API. (For this question , I have hardcode the value). I binded source url instead of adding the value to Source in XAML. But it's not working. There are few solutions in stack and forums. I tried. But didn't work. Someone please help me to sovle this.

这是我的XAML

    <?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyProject.Views.NewRegistration.PrivacyWebView">
    <ContentPage.Content>
        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <WebView Source="{Binding WebViewSource}" HeightRequest= "300" WidthRequest="250" Navigated="Handle_Navigated" VerticalOptions="FillAndExpand" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" />
            <ActivityIndicator x:Name="loader" IsRunning="true" IsVisible="true" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"/>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

这就是我绑定来源的方式. (也在Codebehind和ViewModel中进行了尝试)

This is how I bind the source. (Tried this in Codebehind and ViewModel too)

 public HtmlWebViewSource WebViewSource
{
    get
    {
        return new HtmlWebViewSource { Html = "https://www.stackoverflow.com" };
    }
}  

推荐答案

您使用的是错误的,使用HtmlWebViewSource时,您需要指定实际的HTML而不是要转到的URL.如果要导航到URL,请在Source属性中指定它.

You're using it wrong, when using the HtmlWebViewSource you need to specify actual HTML instead of the URL where you want to go to. If you want to navigate to a URL, specify it in the Source property.

如果要绑定它,则必须实现类似这样的东西.

If you want to bind it, you have to implement something like this.

在您的视图模型中创建一个字符串属性:

In your view model create a string property:

public string UrlToGoTo { get; set; }

然后像平常一样设置它,确保以某种方式实现了INotifyPropertyChanged.

Then set it like you normally would, make sure to have INotifyPropertyChanged is implemented somehow.

然后,像这样连接您的WebView:

Then, wire up your WebView like this:

<WebView Source="{Binding UrlToGoTo}"
    HeightRequest= "300"
    WidthRequest="250"
    Navigated="Handle_Navigated"
    VerticalOptions="FillAndExpand"
    AbsoluteLayout.LayoutFlags="All"
    AbsoluteLayout.LayoutBounds="0,0,1,1" />

这篇关于XAML WebView绑定到字符串在Xamarin Forms中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆