Mvvm十字手柄按钮点击视图模型

本文关键字:按钮 视图 模型 十字 Mvvm | 更新日期: 2023-09-27 17:58:11

我是xamarin和mvvmcross的新手,我想把一个简单的按钮点击从我的ios项目连接到我的视图模型。

using System;
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
using Colingual.Core.ViewModels;
namespace Colingual.iOS
{
    public partial class LoginView : MvxViewController
    {
        public LoginView() : base("LoginView", null)
        {
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            var set = this.CreateBindingSet<LoginView, LoginViewModel>();
            set.Bind(Username).To(vm => vm.Username);
            set.Bind(Password).To(vm => vm.Password);
            set.Bind(btnLogin).To(vm => vm.MyAwesomeCommand);
            set.Apply();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
}

我想把btnlogin接通缅甸司令部。

using MvvmCross.Core.ViewModels;
namespace Colingual.Core.ViewModels
{
    public class LoginViewModel : MvxViewModel
    {
        readonly IAuthenticationService _authenticationService;
        public LoginViewModel(IAuthenticationService authenticationService)
        {
            _authenticationService = authenticationService;
        }
        string _username = string.Empty;
        public string Username
        {
            get { return _username; }
            set { SetProperty(ref _username, value); }
        }
        string _password = string.Empty;
        public string Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }

        public bool AuthenticateUser() {
            return true;
        }
        MvxCommand _myAwesomeCommand;

        public IMvxCommand MyAwesomeCommand
        {
            get
            {
                DoStuff();
                return _myAwesomeCommand;
            }
        }
        void DoStuff()
        {
            string test = string.Empty;
        }
    }
}

正如你所看到的,我有一个名为MyAwesomecommand的mvxCommand,但我想通过在我的另一个项目中点击按钮来处理一些逻辑。有人知道我该怎么做吗?

Mvvm十字手柄按钮点击视图模型

我做了更多的环顾四周,在这里找到了答案。

MvxCommand _myAwesomeCommand;
        public IMvxCommand MyAwesomeCommand
        {
            get { return new MvxCommand(DoStuff); }
        }
        void DoStuff()
        {
            string test = string.Empty;
        }

这个想法是让mvxcommandgetter返回一个新命令,该命令将该方法作为参数。

单击按钮"登录"时,您可以访问视图模型中的void DoStuff。