【发布时间】:2014-10-23 17:33:46
【问题描述】:
我正在尝试将自定义命令绑定到 UIAlertController (ios 8)。我在导航栏中有一个按钮,并附有一个操作表。当用户单击导航栏按钮时,将出现操作表。当用户单击操作表按钮时,他必须被重定向到另一个视图/视图模型。
我的视图模型代码:
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);
});
因此,操作表中的每个按钮都应重定向到特定的视图模型。但似乎操作表中的按钮不是 UIButtons。所以我需要以某种方式将 UIAlertAction 绑定到 ICommand。
set.Bind(...).For(...).To(vm => vm.AddPhonecallCommand);
我应该用什么代替点?
我在视图中没有视图模型的实例。我正在使用这种语法:
var set = this.CreateBindingSet<MainView, MainViewModel>();
set.Bind(source).To(vm => vm.Events); // binding IEnumerable
set.Apply();
没有直接实例化视图模型。该框架为我做了所有肮脏的工作。所以,如果我在写
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 中根本没有合适的活动。
【问题讨论】:
-
我不确定我是否理解您想要实现的“绑定”。您能否编辑问题以解释您尝试绑定的 ViewModel 属性和/或命令的类型。 (我真的不确定您是在尝试绑定导航栏按钮、操作表按钮还是其他东西 - 抱歉!)
-
我已经编辑了问题,希望对您有所帮助。