Mvvmcross:框架布局在 activity 之后不显示片段
Mvvmcross: framelayout doesn't show fragment after activity
所以我有一个片段没有显示在 MvxCachingFragmentCompatActivity
中的问题。
我用来解决问题的模式如下:
- 注册一个activity.
- 导航到另一个扩展 MvxCachingFragmentCompatActivity
的 activity
- 使用
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;
}
}
Main activity:(我觉得没什么特别的)
[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>();
}
}
主要activity布局:(非常简单的布局)
<?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,但我不确定这是我的问题还是他们的问题?
您永远不应该使用 async void
或从非异步命令启动异步任务。这些是首要问题。此外,您的 Initialize 不会被调用,因为您没有使用 RegisterNavigationServiceAppStart<>()
。另一件事是你应该直接导航到片段而不是首先导航到 activity,因为 MvvmCross 会处理它。
另一个提示是使用依赖注入来解决IMvxNavigationService
。
所以我有一个片段没有显示在 MvxCachingFragmentCompatActivity
中的问题。
我用来解决问题的模式如下:
- 注册一个activity.
- 导航到另一个扩展 MvxCachingFragmentCompatActivity 的 activity
- 使用
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;
}
}
Main activity:(我觉得没什么特别的)
[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>();
}
}
主要activity布局:(非常简单的布局)
<?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,但我不确定这是我的问题还是他们的问题?
您永远不应该使用 async void
或从非异步命令启动异步任务。这些是首要问题。此外,您的 Initialize 不会被调用,因为您没有使用 RegisterNavigationServiceAppStart<>()
。另一件事是你应该直接导航到片段而不是首先导航到 activity,因为 MvvmCross 会处理它。
另一个提示是使用依赖注入来解决IMvxNavigationService
。