Mvvmcross:framelayout在活动后不显示片段

时间:2017-08-24 08:21:50

标签: android android-fragments mvvmcross

所以我的问题是片段没有显示在MvxCachingFragmentCompatActivity内。

我用来解决问题的模式如下:

  1. 注册活动。
  2. 导航到另一个扩展MvxCachingFragmentCompatActivity
  3. 的活动
  4. 使用await _navigationService.Navigate<[TheFragmentViewModel]>();
  5. 加载片段
  6. 调用片段加载但不显示任何内容。
  7. 片段声明:

    [MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
    [Register(nameof(FirstFragment))]
    public class FirstFragment : MvxFragment<FirstViewModel>
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.FirstView, container, false);
            return view;
        }
    }
    

    主要活动: (我觉得没什么特别的)

    [Activity(Label = "Fragment View")]
    public class MainActivity : MvxCachingFragmentCompatActivity<MainViewModel>
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.MainView);
        }
    }
    

    主视图模型

    public class MainViewModel : MvxViewModel
    {
        private readonly IMvxNavigationService _navigationService;
    
        public MainViewModel(IMvxNavigationService navigationService)
        {
            _navigationService = navigationService;
            Init();
        }
    
        public async void Init()
        {
            await _navigationService.Navigate<FirstViewModel>();
        }
    }
    

    主要活动布局: (非常简单的布局)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:local="http://schemas.android.com/apk/res-auto"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
      <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout> 
    

    我还在github上添加了一个示例:Github link

    我还添加了a bug report on the Mvvmcross github,但我不确定这是我或他们的错误?

1 个答案:

答案 0 :(得分:2)

永远不要使用async void或从非异步命令启动异步任务。这是第一个问题。此外,您的Initialize未被调用,因为您没有使用RegisterNavigationServiceAppStart<>()。另一件事是你应该直接导航到片段而不是首先导入活动,因为MvvmCross会处理它。

另一个提示是使用依赖注入来解析IMvxNavigationService