【发布时间】:2017-11-17 11:19:02
【问题描述】:
我正在努力在 ViewModel 级别执行从 Fragment 到 Activity 的导航。我有一个带有 DrawerLayout 的 Activity,这个 Activity 有一个 FrameLayout 来显示从 DrawerLayout 中选择的不同片段。该导航由该 Activity 的 ViewModel 执行,它调用每个 Fragment 的 ViewModel 来显示。在其中一个片段中,我添加了一个绑定 IMvxCommand 方法的按钮,以执行从片段到新 Activity 的导航,这就是我遇到问题的地方,因为当我单击按钮时没有任何反应。
在我的代码下方查找。
Fragment的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。
【问题讨论】: