简体   繁体   English

Xamarin iOS - MVVMCross:无法使用ViewModel中的命令连接自定义单元格中的按钮

[英]Xamarin iOS - MVVMCross : Unable to connect button in custom cell with command in ViewModel

I am using MVVMCross, for my Xamarin iOS project. 我正在使用MVVMCross,用于我的Xamarin iOS项目。 I am using a button inside the cell. 我正在使用单元格内的按钮。 I want to connect it to viewModel through command, but for some reason, I am not able to successfully bind them. 我想通过命令将它连接到viewModel,但由于某种原因,我无法成功绑定它们。

Can someone please have a look and let me know where I am doing wrong or what updates should I make to my code. 有人可以看看,让我知道我做错了什么或我应该对我的代码做什么更新。

View: 视图:

public partial class MyCell : MvxCollectionViewCell 
{
        public PostImageCell(IntPtr handle) : base(handle)
        {
            this.DelayBind(() =>
            {
                    var bSet = this.CreateBindingSet<MyCell, SomeViewModel>();
                    bSet.Bind(Btn).To(vm => vm.EditPhotoCommand);
                    bSet.Apply();
            });
        }
}

ViewModel: 视图模型:

public class SomeViewModel : BaseCoreViewModel
{
        public SomeViewModel()
        {
         editPhotoCommand = new BaseMvxCommand(DoShow);
        }

        public ICommand EditPhotoCommand { get { return editPhotoCommand; } }

        private void DoShow()
        { 
          //
        }
}

I kept some breakpoints and realized that when the MyCell constructor is being called, the code inside this.DelayBind() is not being hit, so i tried removing this.DelayBind but still no use. 我保留了一些断点,并意识到当调用MyCell构造函数时, this.DelayBind()的代码没有被命中,所以我尝试删除this.DelayBind但仍然没有用。 Whenever I click on the button inside the cell, its not binding to the command inside ViewModel 每当我单击单元格内的按钮时,它就不会绑定到ViewModel内的命令

Probably do something like below in your ViewModel and then bind as bSet.Bind(Btn).To(vm => vm.ClickCommand); 可能在ViewModel中执行类似下面的操作,然后绑定为bSet.Bind(Btn).To(vm => vm.ClickCommand); I have your button is properly set on the view. 我在视图上正确设置了您的按钮。 I have a sample working right now with button on tableviewcell. 我现在有一个示例正在使用tableviewcell上的按钮。

    private MvxCommand _clickCommand;
    public ICommand ClickCommand
    {
        get
        {
            _clickCommand = _clickCommand ?? new MvxCommand(Hit);
            return _clickCommand;
        }
    }

    private void Hit()
    {
        System.Diagnostics.Debug.WriteLine("Tapped Click Me");
        //System. .WriteLine ("Tapped Click Me");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM