MvvmCross Android:如何为任意MvxViewModel创建片段子视图的绑定?

时间:2014-07-17 12:44:21

标签: android xamarin mvvmcross

我搜索了很长时间,并没有找到任何案例来匹配我目前面临的问题。

我有一个MvxFragmentActivity(MainRootView.cs / .axml)的子类,其中包含一个Android导航抽屉布局:

public class MainRootView : MvxFragmentActivity, IFragmentHost 
    { 
    ...
    protected override void OnViewModelSet () 
        {
            base.OnViewModelSet ();
            SetContentView (Resource.Layout.MainRootView);
            ...
        }

在Setup.cs中,我定义了一个自定义Presenter来控制加载新viewModel时显示的片段:

public interface IFragmentHost
    {
        bool Show (MvxViewModelRequest request);
    }

    public interface ICustomPresenter
    {
        void Register (Type viewModelType, IFragmentHost host);
    }

    public class CustomPresenter : MvxAndroidViewPresenter, ICustomPresenter
    {
        private Dictionary<Type, IFragmentHost> dictionary = new Dictionary<Type, IFragmentHost>();

        public override void Show (MvxViewModelRequest request) 
        {
            IFragmentHost host;
            if (this.dictionary.TryGetValue (request.ViewModelType, out host)) 
            {
                if (host.Show (request)) 
                {
                    return;
                }
            }
            base.Show (request);
        }

        public void Register(Type viewModelType, IFragmentHost host) 
        {
            this.dictionary [viewModelType] = host;
        }
    }

现在 - 在MainRootView.cs中 - 我为某个ViewModelRequest实现了Show方法,并用特定的片段替换了framelayout内容:

public bool Show(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request)
{
    if (request.ViewModelType == typeof(OneCertainViewModel)) 
    {
        var loaderService = Mvx.Resolve<IMvxViewModelLoader> ();
        var viewModel = loaderService.LoadViewModel (request, null);

        var oneCertainFragment = new OneCertainFragment ();
        oneCertainFragment.ViewModel = viewModel;

        var ft = SupportFragmentManager.BeginTransaction ();
        ft.Replace (Resource.Id.flMainContent, oneCertainFragment);
        ft.Commit ();

        return true;
    }
    ...
}

片段代码如下所示:

public class HubFragment : MvxFragment 
    {
    ...
    public override global::Android.Views.View OnCreateView(global::Android.Views.LayoutInflater inflater, 
                                                            global::Android.Views.ViewGroup container, 
                                                            Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            global::Android.Views.View view = this.BindingInflate(Resource.Layout.HubFragment, null);

            return view;
        }
        ...

此片段包含少量动态创建的自定义子视图,每个子视图与单独的视图模型相关,该视图模型在创建后立即设置。

public class SubSectionView : LinearLayout, IHubSectionView
{
    private MySubViewModel _vm;

    public void SetContentViewModel (MySubViewModel vm) 
    {
        _vm = vm;
        ...
    }

现在如何将此子视图内的TextView属性绑定到我之前设置的viewModel的属性,如果可能的话呢?

非常感谢任何建议!

1 个答案:

答案 0 :(得分:0)

我希望了解你的情况,我的问题形式完全相同: 我有一个ViewModel与另一个SubViewModel作为属性;我们称之为:MySubView

假设我们有一个这样的主视图:

<RelativeLayout 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">

...

<Mvx.MvxFrameControl
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  local:MvxBind="DataContext MySubView"
  local:MvxTemplate="@layout/subview"/>

...

</RelativeLayout>

使用MvxFrameControl是关键。它绑定我们的属性并将其传递给指定的模板。我们可以使用SubViewModel的属性。

所以我们需要的是在主视图模型中定义MySubView

public class MainViewModel : MvxViewModel
{
    ...
    private SubViewModel _mySubView = new SubViewModel();
    public SubViewModel MySubView
    { 
        get { return _mySubView; }
        set { _mySubView = value; RaisePropertyChanged(() => MySubView); }
    }
    ...
} 

希望它有所帮助。