如何解决Xamarin.Forms:如何从ViewModel访问WebView的“重新加载”方法
我正在使用 Xamarin.Forms 应用程序,该应用程序包含允许用户安排约会的 WebView :
<WebView x:Name="webView" Source="{Binding UrlBooking}" WidthRequest="1000" HeightRequest="1000" />
我想使用 ToolbarItem 重置/重新加载 WebView 。
我可以通过隐藏代码通过以下方式实现此目标:
this.webView.Reload();
但是我没有看到如何在ViewModel中使用Command来做到这一点。可以这样做吗?
解决方法
您可以使用Command
我的CommandParameter
在ToolbarItem
中实现它。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyCusListview.ToolbarItemPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="reload"
Command="{Binding ReloadWebview}"
CommandParameter="{Binding Source={x:Reference webView}}"
Order="Primary"
Priority="0" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<WebView Source="https://www.google.com/" x:Name="webView" WidthRequest="1000" HeightRequest="1000" ></WebView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
这是布局背景代码。
public partial class ToolbarItemPage : ContentPage
{
public ToolbarItemPage()
{
InitializeComponent();
this.BindingContext = new PersonsViewModel();
}
}
这里是PersonsViewModel.cs
,添加ICommand
,并在PersonsViewModel
的构造函数中实现。
public ICommand ReloadWebview { protected set; get; }
public PersonsViewModel()
{
ReloadWebview = new Command<WebView>((key) =>
{
key.Reload();
});
}
此处正在运行GIF。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。