MvxCachingFragmentStatePagerAdapter将参数传递给viewmodel

时间:2017-07-06 14:07:30

标签: android xamarin mvvmcross

当我为viewpager构建片段列表时,如何将参数传递给我的viewmodel?

我尝试使用" parametervaluesobject" in" MvxCachingFragmentStatePagerAdapter.FragmentInfo"但这似乎不起作用。

[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register("mvvmcrosslearning.droid.fragments.DateFragment")]
public class DateFragment : BaseFragment<DateViewModel>
{
    protected override int FragmentId => Resource.Layout.fragment_date;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);

        var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
        if (viewPager != null)
        {
            var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>();
            for (int i = 0; i < 3; i++)
                fragments.Add(new MvxCachingFragmentStatePagerAdapter.FragmentInfo($"Date {i}",
                    typeof(RecyclerViewFragment), typeof(RecyclerViewModel), new Device() { Date = $"Date {i}" }));


            viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(Activity,ChildFragmentManager,fragments);
            viewPager.OffscreenPageLimit = 1;
        }

        var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewPager);

        return view;

    }
}

1 个答案:

答案 0 :(得分:1)

谢谢约克沉。我找到了如何在ViewModel中获取数据: 只需在RecyclerViewModel的ViewModel中覆盖该方法:

    protected override void InitFromBundle(IMvxBundle parameters)
    {
        base.InitFromBundle(parameters);
        Device = new Device()
        {
            Date = parameters.Data["Date"]
        };
        RaisePropertyChanged(() => Device);
    }