我正在尝试在单击按钮时导航到新视图模型的按钮上设置绑定。
这是我的ViewModel:
public class NotificationsViewModel : BaseViewModel
{
public NotificationsViewModel(IMvxNavigationService navigation)
: base(navigation)
{
}
public void GoToNextPage()
{
_navigation.Navigate<AboutMeViewModel>();
}
public MvxCommand GoToNextPageCommand
{
get { return new MvxCommand(async () => await _navigation.Navigate<AboutMeViewModel>()); }
}
}
这是它继承的BaseViewModel的构造函数(我不认为BaseViewModel的其余部分是相关的,但请告诉我它是否有用):
public abstract class BaseViewModel : MvxViewModel, IBaseViewModel
{
protected readonly IMvxNavigationService _navigation;
public BaseViewModel(IMvxNavigationService navigation)
{
_navigation = navigation;
}
}
以下是我创建与此ViewModel绑定的视图:
public partial class NotificationsView : BaseView<NotificationsViewModel>
{
public NotificationsView() : base("NotificationsView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var set = this.CreateBindingSet<NotificationsView, NotificationsViewModel>();
set.Bind(NextPageButton.Tap()).For(tap => tap.Command).To(nameof(ViewModel.GoToNextPage));
set.Bind(NextPageCommandButton).To(nameof(ViewModel.GoToNextPageCommand));
set.Apply();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
[Register ("NotificationsView")]
partial class NotificationsView
{
[Outlet]
[GeneratedCode ("iOS Designer", "1.0")]
UIKit.UIButton NextPageButton { get; set; }
void ReleaseDesignerOutlets ()
{
if (NextPageButton != null) {
NextPageButton.Dispose ();
NextPageButton = null;
}
}
}
以下是它继承的BaseView:
public abstract class BaseView<T> : MvxViewController<T>, IBaseView
where T : BaseViewModel
{
private object _headerToken = null;
private object _addTaskToken = null;
private UIView _originalView = null;
public BaseView(string name, NSBundle bundle)
: base(name, bundle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (ViewModel == null)
return;
_headerToken = ViewModel.WeakSubscribe(() => ViewModel.IsLogoVisible, OnHeaderEnabledChanged);
_addTaskToken = ViewModel.WeakSubscribe(() => ViewModel.IsAddTaskVisible, OnAddTaskEnabledChanged);
ViewModel.Start();
}
}
当我在模拟器中调试此代码时,我可以在被GoToNextPage
内部设置一个断点,因此我知道绑定正在捕获click事件。但是,当我继续执行时,不会发生导航到新视图模型,但不会抛出异常。
我能够在没有其他页面问题的情况下导航到此视图模型,因此我认为设置此绑定的方式一定有问题。据我所知,这种绑定设置与我正在使用的绑定完全相同。我在这里错过了什么?
答案 0 :(得分:0)
您是否缺少用于注册视图的属性以及用于将视图映射到视图模型的属性?
[Register("NotificationsView")] // this is missing...
[MvxViewFor(typeof(NotificationsViewModel))] // this is missing...
public partial class NotificationsView : BaseView<NotificationsViewModel>
{
public NotificationsView() : base("NotificationsView", null)
{
}
}