首页 文章

从Fragment导航到Activity MVVMCross

提问于
浏览
0

我正在努力在ViewModel级别执行从Fragment到Activity的导航 . 我有一个带有DrawerLayout的Activity,这个Activity有一个FrameLayout来显示从DrawerLayout中选择的不同Fragment . 该导航由此Activity的ViewModel执行,它调用每个Fragment的ViewModel进行显示 . 在其中一个片段中,我添加了一个绑定IMvxCommand方法的按钮来执行从Fragment到新Activity的导航,这里是我遇到问题的地方,因为当我点击按钮时没有任何反应 .

找到我的代码 .

ViewModel of Fragment

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;
            }
        }
    }

Fragment code

[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);
        }
    }

Activity to navigate

[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 .

1 回答

  • 2

    您的绑定无法正常工作,因为您使用的是默认的inflater,它对MvvmCross绑定一无所知 . 您可以使用 OnCreateView 中的MvvmCross inflater来解决此问题 . 更改 return inflater.Inflate(Resource.Layout.MainFrame, container, false); 致电 return this.BindingInflate(Resource.Layout.MainFrame, null);

    此外,您忽略了 IMvxNavigationService 的异步部分 . 从 IMvxCommand 更改为 IMvxAsyncCommandawait 或返回 IMvxNavigationService.Navigate() 返回的 Task 将是一种改进

相关问题