gpt4 book ai didi

ios - mvvmcross 中的 UIAlertController 绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 12:35:17 25 4
gpt4 key购买 nike

我正在尝试将自定义命令绑定(bind)到 UIAlertController (ios 8)。我在导航栏中有一个按钮,并附有一个操作表。当用户单击导航栏按钮时,将出现操作表。当用户单击操作表按钮时,他必须被重定向到另一个 View / View 模型。

我的 View 模型代码:

public ICommand AddPhonecallCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AddPhonecallViewModel>();
}
}

public ICommand AddMeetingCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AddMeetingViewModel>();
}
}

我的查看代码:

var actionSheet = UIAlertController.Create("Add a new...", null, UIAlertControllerStyle.ActionSheet);

actionSheet.AddAction(UIAlertAction.Create("Phone call", UIAlertActionStyle.Default, null));
actionSheet.AddAction(UIAlertAction.Create("Meeting", UIAlertActionStyle.Default, null));

var rightNavButton = new UIBarButtonItem("Add", UIBarButtonItemStyle.Plain, (s, e) =>
{
this.PresentViewController(actionSheet, true, null);
});

因此,操作表中的每个按钮都应重定向到特定的 View 模型。但似乎操作表中的按钮不是 UIButtons。所以我需要以某种方式将 UIAlertAction 绑定(bind)到 ICommand。

set.Bind(...).For(...).To(vm => vm.AddPhonecallCommand);

我应该用什么代替点?


我在 View 中没有 View 模型的实例。我正在使用这种语法:

var set = this.CreateBindingSet<MainView, MainViewModel>();
set.Bind(source).To(vm => vm.Events); // binding IEnumerable
set.Apply();

没有 View 模型的直接实例化。该框架为我做了所有肮脏的工作。所以,如果我在写

set.Bind(rightNavButton).To(vm => vm.AddPhonecallCommand);  // binding the Clicked event

一切正常。但是如果我想写这样的东西

var actionSheetButton = UIAlertAction.Create("Phone call", UIAlertActionStyle.Default, null));
...
set.Bind(actionSheetButton).To(vm => vm.AddPhonecallCommand); // attempt to bind

没有任何反应。可能是因为我们在 UIAlertAction 中根本没有合适的事件。

最佳答案

如果您只想执行这些命令,那么一个简单的方法是使用:

 actionSheet.AddAction(UIAlertAction.Create("Phone call", UIAlertActionStyle.Default, (s,e) => {
MyViewModel.AddPhoneCallCommand.Execute(null);
}));

如果您想要“完全绑定(bind)”——包括观察 View 模型更改和观察 CanExecute 更改,那么也可以这样做——您需要保留 UIAlertAction 实例并将它们的属性绑定(bind)到 ViewModel。

关于ios - mvvmcross 中的 UIAlertController 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533956/

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