所以我的问题是片段没有显示在MvxCachingFragmentCompatActivity
内。
我用来解决问题的模式如下:
await _navigationService.Navigate<[TheFragmentViewModel]>();
片段声明:
[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,但我不确定这是我或他们的错误?
答案 0 :(得分:2)
永远不要使用async void
或从非异步命令启动异步任务。这是第一个问题。此外,您的Initialize未被调用,因为您没有使用RegisterNavigationServiceAppStart<>()
。另一件事是你应该直接导航到片段而不是首先导入活动,因为MvvmCross会处理它。
另一个提示是使用依赖注入来解析IMvxNavigationService
。