当前位置 : 主页 > 手机开发 > ios >

MvvmCross iOS SelectedCommand用于自定义TableViewCell

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在研究iOS的MvvmCross应用程序,我需要使用自定义的TableViewCell.我想知道如何在MvxSimpleTableViewSource上实现SelectedCommand事件.我得到它来工作MvxStandardTableViewSource,我在youtube上找到了 this解决
我正在研究iOS的MvvmCross应用程序,我需要使用自定义的TableViewCell.我想知道如何在MvxSimpleTableViewSource上实现SelectedCommand事件.我得到它来工作MvxStandardTableViewSource,我在youtube上找到了 this解决方案.该解决方案的问题在于Stuart使用MvxStandardTableViewSource.另一个问题是我想要将SelectedChangedCommand绑定到的模型与我绑定源的模型不同.

我一直在网上搜索一段时间来找到这个问题的答案,所以我希望有人可以帮助我.提前致谢.

这是我的代码:

MainViewModel.cs

namespace MyApp.Core.ViewModels
{
    public class MainViewModel : BaseViewModel
    {
        private NavigationListViewModel _navigationMenu;
        public NavigationListViewModel NavigationMenu
    {
        get { return _navigationMenu; }
        set { _navigationMenu = value; RaisePropertyChanged(() => NavigationMenu); }
    }

    public void Init()
    {
        NavigationMenu = new NavigationListViewModel();
        NavigationMenu.Init();
    }
}

NavigationListViewModel.cs

namespace MyApp.Core.ViewModels.NavigationViewModels
{
public class NavigationListViewModel : BaseViewModel, IPageSelectedService
{
    private List<NavigationItemViewModel> _navigationList;
    public List<NavigationItemViewModel> NavigationList { 
        get { return _navigationList; } 
        set { _navigationList = value; RaisePropertyChanged(() => NavigationList); } 
    }

    public void Init() {
        NavigationList = new List<NavigationItemViewModel> {
            new NavigationItemViewModel { DisplayName = "Assortment", ActionLink = NavigateToCategories },
            new NavigationItemViewModel { DisplayName = "Shops", ActionLink = NavigateToShops }
        };
    }

    public  ICommand NavigateToCategories 
    {
        get { return new MvxCommand(() => ShowViewModel<CategoryListViewModel>()); }
    }
    public ICommand NavigateToShops
    {
        get { return new MvxCommand(() => ShowViewModel<StoreListViewModel>()); }
    }
}

NavigationItemViewModel.cs

namespace MyApp.Core.ViewModels.NavigationViewModels
{
    public class NavigationItemViewModel : MvxViewModel
    {   
        private string _displayName;
        public string DisplayName
        { 
            get { return _displayName; }
            set { _displayName = value; RaisePropertyChanged(() => DisplayName); }
        }

        private ICommand _actionLink;
        public ICommand ActionLink { 
            get { return _actionLink; } 
            set { _actionLink = value; RaisePropertyChanged(() => ActionLink); }
        }
    }
}

MainView.cs

namespace MyApp.iOS.Views 
{
    public partial class MainView : MvxViewController 
    {
        public new MainViewModel ViewModel {
            get { return (MainViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }

        public MainView() : base ("MainView", null) 
        {
        }

        public override void ViewDidLoad() {
            base.ViewDidLoad();

            var set = this.CreateBindingSet<MainView, MainViewModel>();
            var source = new MvxSimpleTableViewSource(menuTableView, MainTableCell.Key, MainTableCell.Key);

            menuTableView.Source = source;
            set.Bind(source).To(vm => vm.NavigationMenu.NavigationList);
            set.Apply();

            menuTableView.ReloadData();
        }
    }
}

MainTableCell

namespace MyApp.iOS.Views {
    public partial class MainTableCell : MvxTableViewCell {
        public static readonly UINib Nib = UINib.FromName("MainTableCell", NSBundle.MainBundle);
        public static readonly NSString Key = new NSString("MainTableCell");

        public MainTableCell(IntPtr handle) : base (handle) {
            this.DelayBind(() => {
                var set = this.CreateBindingSet<MainTableCell, NavigationItemViewModel>();
                set.Bind(titleLabel).To(vm => vm.DisplayName);
                set.Apply();
            });
        }

        public static MainTableCell Create() {
            return (MainTableCell)Nib.Instantiate(null, null)[0];
        }
    }
}
您可以将SelectedCommand属性添加到单元格实现中,将其绑定到ActionLink vm属性,并在用户使用 https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxStandardTableViewCell.cs中的SetSelected中的代码选择单元格时执行该属性

public ICommand SelectedCommand { get; set; }


    private bool _isSelected;


    public override void SetSelected(bool selected, bool animated)
    {
        base.SetSelected(selected, animated);


        if (_isSelected == selected)
            return;


        _isSelected = selected;
        if (_isSelected)
            if (SelectedCommand != null)
                SelectedCommand.Execute(null);
    }
网友评论