如何使用 MVVM 将 XAML 标签与 TapGestureRecognizer 绑定?
How do I bind XAML Label with TapGestureRecognizer using MVVM?
我正在尝试使用 Frame
(用于边框和背景)和 Label
(用于按钮文本)来模拟 XAML 按钮。但是,我需要添加一个 TapGestureRecognizer
才能使模拟按钮在单击时起作用。我试图在使用 ReactiveUI 和 MVVM 的范围内执行此操作,但似乎无法正确连接绑定。
我正在关注 this tutorial,它在后面的 XAML 代码中设置绑定,如下所示:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPageBase<LoginViewModel>
{
public LoginPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
protected override void OnAppearing()
{
base.OnAppearing();
this.WhenActivated(disposables =>
{
this.Bind(ViewModel, vm => vm.Username, c => c.UsernameEntry.Text)
.DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.Password, c => c.PasswordEntry.Text)
.DisposeWith(disposables);
//this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.LoginButton.LoginCommand)
// .DisposeWith(disposables);
this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.SimulatedLoginButton.TapGestureCommandGoesHere)
.DisposeWith(disposables);
this.OneWayBind(ViewModel, x => x.IsLoading, x => x.LoginActivityIndicator.IsRunning)
.DisposeWith(disposables);
});
}
我已经注释掉了 Button
绑定(上面)并插入了模拟的 Button
代码(当然不能编译)。问题是模拟按钮(框架)没有 button.Command
,我不确定如何添加 taggesture 命令以便它被识别。
我有 seen/tried XAML 和 Frame.TapGestureRecognizer
标签,但无法使用此方法使绑定与 XAML 一起工作。
有什么想法可以使它与 MVVM 实现一起使用吗?
在你的XAML中你必须有这样的东西:
<Frame>
<Frame.GestureRecognizers>
<TapGestureRecognizer x:Name="Gesture" />
</Frame.GestureRecognizers>
</Frame>
在你后面的代码中:
this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.Gesture.Command)
.DisposeWith(disposables);
希望对您有所帮助
我正在尝试使用 Frame
(用于边框和背景)和 Label
(用于按钮文本)来模拟 XAML 按钮。但是,我需要添加一个 TapGestureRecognizer
才能使模拟按钮在单击时起作用。我试图在使用 ReactiveUI 和 MVVM 的范围内执行此操作,但似乎无法正确连接绑定。
我正在关注 this tutorial,它在后面的 XAML 代码中设置绑定,如下所示:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPageBase<LoginViewModel>
{
public LoginPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
protected override void OnAppearing()
{
base.OnAppearing();
this.WhenActivated(disposables =>
{
this.Bind(ViewModel, vm => vm.Username, c => c.UsernameEntry.Text)
.DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.Password, c => c.PasswordEntry.Text)
.DisposeWith(disposables);
//this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.LoginButton.LoginCommand)
// .DisposeWith(disposables);
this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.SimulatedLoginButton.TapGestureCommandGoesHere)
.DisposeWith(disposables);
this.OneWayBind(ViewModel, x => x.IsLoading, x => x.LoginActivityIndicator.IsRunning)
.DisposeWith(disposables);
});
}
我已经注释掉了 Button
绑定(上面)并插入了模拟的 Button
代码(当然不能编译)。问题是模拟按钮(框架)没有 button.Command
,我不确定如何添加 taggesture 命令以便它被识别。
我有 seen/tried XAML 和 Frame.TapGestureRecognizer
标签,但无法使用此方法使绑定与 XAML 一起工作。
有什么想法可以使它与 MVVM 实现一起使用吗?
在你的XAML中你必须有这样的东西:
<Frame>
<Frame.GestureRecognizers>
<TapGestureRecognizer x:Name="Gesture" />
</Frame.GestureRecognizers>
</Frame>
在你后面的代码中:
this.OneWayBind(ViewModel, x => x.LoginCommand, x => x.Gesture.Command)
.DisposeWith(disposables);
希望对您有所帮助