我是 C# 和 Xamarin Forms 的新手。我有一个 webview 并从 API 获取源 url。 (对于这个问题,我已经对值进行了硬编码)。我绑定(bind)了源 url,而不是将值添加到 XAML 中的 Source。但这不起作用。堆栈和论坛中的解决方案很少。我试过。但没有成功。请有人帮我解决这个问题。
这是我的 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>
这就是我绑定(bind)源的方式。 (也在 Codebehind 和 ViewModel 中尝试过这个)
public HtmlWebViewSource WebViewSource
{
get
{
return new HtmlWebViewSource { Html = "https://www.stackoverflow.com" };
}
}
最佳答案
你用错了,当使用HtmlWebViewSource
时,你需要指定实际的HTML而不是你想要访问的URL。如果您想要导航到某个 URL,请在 Source
属性中指定它。
如果你想绑定(bind)它,你必须实现这样的东西。
在 View 模型中创建一个字符串属性:
public string UrlToGoTo { get; set; }
然后像平常一样设置它,确保以某种方式实现了INotifyPropertyChanged
。
然后,像这样连接您的 WebView
:
<WebView Source="{Binding UrlToGoTo}"
HeightRequest= "300"
WidthRequest="250"
Navigated="Handle_Navigated"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1" />
关于c# - XAML WebView 绑定(bind)到字符串在 Xamarin Forms 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53099447/