gpt4 book ai didi

ios - 用于自定义 TableViewCell 的 MvvmCross iOS SelectedCommand

转载 作者:可可西里 更新时间:2023-11-01 17:08:36 26 4
gpt4 key购买 nike

我正在开发适用于 iOS 的 MvvmCross 应用程序,我需要在其中使用自定义 TableViewCell。我想知道如何在 MvxSimpleTableViewSource 上实现 SelectedCommand 事件。我让它在 MvxStandardTableViewSource 上工作,我发现 this youtube 上的解决方案。该解决方案的问题在于 Stuart 使用 MvxStandardTableViewSource。另一个问题是我要将 SelectedChangedCommand 绑定(bind)到的模型与我将源绑定(bind)到的模型不同。

我一直在网上搜索这个问题的答案,所以我希望有人能帮助我解决这个问题。提前致谢。

这是我的代码:

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 属性添加到您的单元格实现,将其绑定(bind)到您的 ActionLink 虚拟机属性,并在用户使用类似于 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);
}

关于ios - 用于自定义 TableViewCell 的 MvvmCross iOS SelectedCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com