我正在努力在ViewModel级别执行从Fragment到Activity的导航。我有一个带有DrawerLayout的Activity,这个Activity有一个FrameLayout来显示从DrawerLayout中选择的不同Fragment。该导航由此Activity的ViewModel执行,它调用每个Fragment的ViewModel进行显示。在其中一个片段中,我添加了一个绑定IMvxCommand方法的按钮,用于执行从Fragment到新Activity的导航,这就是我遇到问题的地方,因为当我点击按钮时没有任何反应。
在下面查找我的代码。
片段的ViewModel
public class MainFrameViewModel : ContentViewModel
{
readonly IMvxNavigationService navigationService;
public MainFrameViewModel(IMvxNavigationService navigationService) : base(navigationService)
{
this.navigationService = navigationService;
}
public IMvxCommand GoMoreInfo
{
get
{
IMvxCommand navigateCommand = new MvxCommand(() => navigationService.Navigate<MoreInfoViewModel>());
return navigateCommand;
}
}
}
片段代码
[MvxFragmentPresentation(typeof(ContentViewModel), Resource.Id.frameLayout)]
[Register("mvvmdemo.droid.views.fragments.MainFrameFragment")]
public class MainFrameFragment : MvxFragment<MainFrameViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.MainFrame, container, false);
}
}
导航活动
[MvxActivityPresentation]
[Activity(Label = "MoreInfoActivity")]
public class MoreInfoActivity : MvxAppCompatActivity<MoreInfoViewModel>
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MoreInfoLayout);
}
}
ContentViewModel是包含FrameLayout和DrawerLayout的Activity的ViewModel。
答案 0 :(得分:2)
您的绑定无效,因为您使用的是默认的inflater,它对MvvmCross绑定一无所知。您可以使用OnCreateView
内的MvvmCross inflater来解决此问题。将return inflater.Inflate(Resource.Layout.MainFrame, container, false);
来电更改为return this.BindingInflate(Resource.Layout.MainFrame, null);
此外,您忽略了IMvxNavigationService
的异步部分。从IMvxCommand
更改为IMvxAsyncCommand
和await
或返回Task
返回的IMvxNavigationService.Navigate()