我正在尝试使用 Frame
模拟 XAML 按钮(用于边框和背景)和 Label
(用于按钮文本)。但是,我需要添加一个 TapGestureRecognizer
模拟按钮在单击时起作用。我试图在使用 ReactiveUI 和 MVVM 的范围内执行此操作,但似乎无法正确连接绑定(bind)。
我关注 this tutorial ,它在后面的 XAML 代码中设置绑定(bind),如下所示:
[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
绑定(bind)(上)并插入模拟的Button
代码(当然不会编译)。问题是模拟的按钮(框架)没有 button.Command
,而且我不确定如何添加 tapgesture 命令以使其被识别。我已经看到/尝试了 XAML 与
Frame.TapGestureRecognizer
标记,但无法使用此方法使绑定(bind)与 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);
我希望这可以帮助你
关于xaml - 如何使用 MVVM 将 XAML 标签与 TapGestureRecognizer 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995355/