WebView.Navigated 和视图模型
WebView.Navigated and ViewModel
在视图模型中使用 Prism 时,是否可以捕获 Xamarin Form 中 WebView 控件的导航事件?我的控件定义如下所示
<WebView Source="{Binding GatewayPageSource,Mode=TwoWay}" WidthRequest="500" HeightRequest="500" />
WebView 有一个名为 Navigated 的事件,您可以使用 EventToCommandBehaviour
将此事件绑定到您的 ViewModels 命令
WebView 对 MVVM 不是特别友好,因为它没有内置命令。不过,有几种方法可以解决这个问题。
1) 将 WebView 扩展为自定义控件,其中包括与您希望处理的事件关联的命令的 BindableProperties。类似下面。
public class MvvmWebView : WebView
{
public static readonly BindableProperty NavigatingCommandProperty =
BindableProperty.Create( nameof( NavigatingCommand ), typeof( ICommand ), typeof( MvvmWebView ), null );
public static readonly BindableProperty NavigatedCommandProperty =
BindableProperty.Create( nameof( NavigatedCommand ), typeof( ICommand ), typeof( MvvmWebView ), null );
public MvvmWebView()
{
Navigating += ( s, e ) =>
{
if( NavigatingCommand?.CanExecute( e ) ?? false )
NavigatingCommand.Execute( e );
};
Navigated += ( s, e ) => {
if( NavigatedCommand?.CanExecute( e ) ?? false )
NavigatedCommand.Execute( e );
}
}
public ICommand NavigatingCommand
{
get { return ( ICommand )GetValue( NavigatingCommandProperty ); }
set { SetValue( NavigatingCommandProperty, value ); }
}
public ICommand NavigatedCommand
{
get { return ( ICommand )GetValue( NavigatedCommandProperty ); }
set { SetValue( NavigatedCommandProperty, value ); }
}
}
2) 您可以向 WebView 添加类似于来自 Xamarin as already mentioned. It's also worth noting that Prism Forms 6.3.0-pre2 will come with the EventToCommandBehavior 的博客 post 的行为,并且应该很快可用。
在视图模型中使用 Prism 时,是否可以捕获 Xamarin Form 中 WebView 控件的导航事件?我的控件定义如下所示
<WebView Source="{Binding GatewayPageSource,Mode=TwoWay}" WidthRequest="500" HeightRequest="500" />
WebView 有一个名为 Navigated 的事件,您可以使用 EventToCommandBehaviour
将此事件绑定到您的 ViewModels 命令WebView 对 MVVM 不是特别友好,因为它没有内置命令。不过,有几种方法可以解决这个问题。
1) 将 WebView 扩展为自定义控件,其中包括与您希望处理的事件关联的命令的 BindableProperties。类似下面。
public class MvvmWebView : WebView
{
public static readonly BindableProperty NavigatingCommandProperty =
BindableProperty.Create( nameof( NavigatingCommand ), typeof( ICommand ), typeof( MvvmWebView ), null );
public static readonly BindableProperty NavigatedCommandProperty =
BindableProperty.Create( nameof( NavigatedCommand ), typeof( ICommand ), typeof( MvvmWebView ), null );
public MvvmWebView()
{
Navigating += ( s, e ) =>
{
if( NavigatingCommand?.CanExecute( e ) ?? false )
NavigatingCommand.Execute( e );
};
Navigated += ( s, e ) => {
if( NavigatedCommand?.CanExecute( e ) ?? false )
NavigatedCommand.Execute( e );
}
}
public ICommand NavigatingCommand
{
get { return ( ICommand )GetValue( NavigatingCommandProperty ); }
set { SetValue( NavigatingCommandProperty, value ); }
}
public ICommand NavigatedCommand
{
get { return ( ICommand )GetValue( NavigatedCommandProperty ); }
set { SetValue( NavigatedCommandProperty, value ); }
}
}
2) 您可以向 WebView 添加类似于来自 Xamarin as already mentioned. It's also worth noting that Prism Forms 6.3.0-pre2 will come with the EventToCommandBehavior 的博客 post 的行为,并且应该很快可用。