gpt4 book ai didi

android - Mvvmcross:框架布局在 Activity 后不显示 fragment

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:10 25 4
gpt4 key购买 nike

所以我有一个 fragment 没有显示在 MvxCachingFragmentCompatActivity 中的问题.

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

  1. 注册一个 Activity 。
  2. 导航到另一个扩展 MvxCachingFragmentCompatActivity 的 Activity
  3. 使用 await _navigationService.Navigate<[TheFragmentViewModel]>(); 加载 fragment
  4. 已调用 fragment 加载,但未显示任何内容。

fragment 声明:

[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 :(我认为没什么特别的)

[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<>() .另一件事是您应该直接导航到 fragment 而不是首先导航到 Activity ,因为 MvvmCross 会处理它。

另一个提示是使用依赖注入(inject)来解决 IMvxNavigationService .

关于android - Mvvmcross:框架布局在 Activity 后不显示 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com