2017-08-24 71 views
2

所以我有一个片段没有显示在MvxCachingFragmentCompatActivity内部的问题。Mvvmcross:framelayout在活动后不显示片段

我用它来找到问题的模式如下:

  1. 注册一个活动。
  2. 导航到扩展MvxCachingFragmentCompatActivity
  3. 负载使用await _navigationService.Navigate<[TheFragmentViewModel]>();
  4. 片段装载的片段另一个活动的调用,但它并没有显示任何东西。

片段声明:

[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,但我不确定这是我的一部分还是他们的错误?

回答

2

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

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

+0

我开始对所有事情都没有足够的理解,对于你的人来说,我仍然在学习一点:) – kevingoos