MvvmCross iOS为自定义UITableViewCell选择Command

时间:2013-08-20 08:08:32

标签: ios xamarin mvvmcross

我正在开发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];
        }
    }
}

2 个答案:

答案 0 :(得分:6)

您可以将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);
    }

答案 1 :(得分:1)

Stuart非常好。这就是我约束它的方式:

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