将数据传递给片段视图模型mvvmcross

时间:2018-02-13 17:15:53

标签: android-fragments mvvmcross

所以我的活动有一个带有2个片段的标签页。

  public class RecipeDetailActivity : BaseFragmentActivity<RecipeDetailViewModel>
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.RecipeDetailView);

            AttachActionBar();
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.Title = "Recipe details";

            var viewPager = FindViewById<ViewPager>(Resource.Id.main_view_pager);

            if (viewPager != null)
            {
                var fragments = new List<MvxViewPagerFragmentInfo>();
                fragments.Add(
                    new MvxViewPagerFragmentInfo("Ingrediente", typeof(RecipeFlavoursFragment), typeof(RecipeFlavoursViewModel)));
                fragments.Add(
                    new MvxViewPagerFragmentInfo("Flavours", typeof(RecipeIngridientsFragment), typeof(RecipeIngridientsViewModel)));

                viewPager.Adapter = new MvxFragmentPagerAdapter(this, SupportFragmentManager, fragments);


                viewPager.Adapter = new MvxFragmentPagerAdapter(this, SupportFragmentManager, fragments);
                var tabLayout = FindViewById<TabLayout>(Resource.Id.main_tablayout);
                tabLayout.SetupWithViewPager(viewPager);
            }
        }
    }

我使用以下代码显示此页面。

   private void SelectRecipe(RecipeModel recipe)
   {
            var recipeJson = JsonConvert.SerializeObject(recipe);

            ShowViewModel<RecipeDetailViewModel>(new { recipe = recipeJson });
   }

现在我想要的是将一些数据传递给子视图模型。 的 RecipeFlavoursViewModel RecipeIngridientsViewModel

我到目前为止尝试过: 使用 parameterValueObject

        fragments.Add(
            new MvxViewPagerFragmentInfo("Ingrediente", typeof(RecipeFlavoursFragment), typeof(RecipeFlavoursViewModel), new { recipe = ViewModel.Recipe }));

使用IMvxBundle

RecipeDetailViewModel

 protected override void SaveStateToBundle(IMvxBundle bundle)
 {
            bundle.Data["Recipe"] = JsonConvert.SerializeObject(Recipe);

            base.SaveStateToBundle(bundle);
 }

RecipeIngridientsViewModel

protected override void InitFromBundle(IMvxBundle parameters)
        {
            base.InitFromBundle(parameters);

            if (parameters.Data.Count != 0)
            {
                Recipe = JsonConvert.DeserializeObject<RecipeModel>(parameters.Data["recipe"]);
            }
        }

到目前为止,他们都没有工作过。有什么想法我做错了什么?我是否必须使用MvvmCross 5中的导航服务才能使用InitFromBundle和SaveStateToBundle。

每次显示我的片段时都会调用

InitFromBundle ,但 RecipeDetailViewModel 中的 SaveStateToBundle 永远不会被调用。

1 个答案:

答案 0 :(得分:4)

为了做到这一点,您可以利用MvxViewPagerFragmentPresentationAttribute,以便Presenter负责显示片段,您只需显示传递Recipe参数的ViewModel除了它之外还有一些小错误。

然而,解决此问题的一种方法是在RecipeDetailViewModel属性中加入片段&#39;您希望在ViewPager中添加ViewModel并将其加载到Initialize中,以便您可以从RecipeDetailActivity引用它们:

使用Mvx 5,您可以使用新的Navigation来显示ViewModel。如果详细信息从RecipeListViewModel开启,则:

public class RecipeDetailViewModelArgs
{
    public RecipeDetailViewModelArgs(RecipeModel recipe)
    {
        this.Recipe = recipe;
    }

    public RecipeModel Recipe { get; }
}

public class RecipeListViewModel : MvxViewModel
{
    private readonly IMvxNavigationService navigationService;

    public RecipeListViewModel(IMvxNavigationService navigationService)
    {
        this.navigationService = navigationService;
    }

    private async Task SelectRecipe(RecipeModel recipe)
    {
        await this.navigationService.Navigate<RecipeDetailViewModel, RecipeDetailViewModelArgs>(new RecipeDetailViewModelArgs(recipe));
    }
}

然后在您的详细信息ViewModel中,您只需缓存配方,加载子ViewModel(成分和口味)并将配方设置为:

public class RecipeDetailViewModel : MvxViewModel<RecipeDetailViewModelArgs>
{
    private readonly IMvxViewModelLoader mvxViewModelLoader;
    private readonly IMvxJsonConverter jsonConverter;
    private RecipeModel recipe;

    public RecipeDetailViewModel(IMvxViewModelLoader mvxViewModelLoader, IMvxJsonConverter jsonConverter)
    {
        this.mvxViewModelLoader = mvxViewModelLoader;
        this.jsonConverter = jsonConverter;
    }

    public override void Prepare(RecipeDetailViewModelArgs parameter)
    {
        this.recipe = parameter.Recipe;
    }

    protected override void SaveStateToBundle(IMvxBundle bundle)
    {
        base.SaveStateToBundle(bundle);

        bundle.Data["RecipeKey"] = this.jsonConverter.SerializeObject(this.recipe);
    }

    protected override void ReloadFromBundle(IMvxBundle state)
    {
        base.ReloadFromBundle(state);

        this.recipe = this.jsonConverter.DeserializeObject<RecipeModel>(state.Data["RecipeKey"]);
    }

    public override async Task Initialize()
    {
        await base.Initialize();

        this.InitializeChildrenViewModels();
    }

    public RecipeFlavoursViewModel FlavoursViewModel { get; private set; }

    public RecipeIngridientsViewModel IngredientsViewModel { get; private set; }

    protected virtual void InitializeChildrenViewModels()
    {
        // Load each childre ViewModel and set the recipe
        this.FlavoursViewModel = this.mvxViewModelLoader.LoadViewModel(new MvxViewModelRequest<RecipeFlavoursViewModel>(null, null), null);
        this.FlavoursViewModel.Recipe = this.recipe;

        this.IngredientsViewModel = this.mvxViewModelLoader.LoadViewModel(new MvxViewModelRequest<RecipeIngridientsViewModel>(null, null), null);
        this.FlavoursViewModel.Recipe = this.recipe;
    }
}

然后当您加载ViewPager时,您可以利用MvxViewPagerFragmentInfo =&gt;的其他构造函数。 public MvxViewPagerFragmentInfo (string title, string tag, Type fragmentType, IMvxViewModel viewModel, object parameterValuesObject = null)所以您可以传递之前加载的ViewModel:

this.viewPager = view.FindViewById<ViewPager>(Resource.Id.viewPagerDetails);
if (viewPager != null)
{
    var fragments = new List<MvxViewPagerFragmentInfo>();
    fragments.Add(new MvxViewPagerFragmentInfo("Ingredients", "RecipeIngridientsViewModelTag", typeof(RecipeIngridientsView), this.ViewModel.IngridientsViewModel));
    fragments.Add(new MvxViewPagerFragmentInfo("Flavours", "RecipeFlavoursViewModelTag", typeof(RecipeFlavoursView), this.ViewModel.FlavoursViewModel));

    this.viewPager.Adapter = new MvxFragmentPagerAdapter(this.Activity, this.ChildFragmentManager, fragments);
}

那就是它。

顺便说一句,如果您不想使用导航,或者您没有使用Mvx 5.x,那么您只需使用void Start()方法初始化子视图模型。

如果你想从孩子们改变你的Recipe的值,一个简单的方法是用你的Recipe初始化Singleton,然后你只需在构造函数中注入单例,这样你就可以得出结论总是有相同食谱的参考,你不必向前传递食谱对象&amp;返回到那些ViewModel并合并从每个ViewModel进行的更改。 MvvmCross: Accessing models by reference from everywhere

中的更多信息

HIH