我找了好久,没有找到符合我目前面临的问题的案例。
我有一个包含 Android 抽屉导航布局的 MvxFragmentActivity (MainRootView.cs/.axml) 的子类:
public class MainRootView : MvxFragmentActivity, IFragmentHost
{
...
protected override void OnViewModelSet ()
{
base.OnViewModelSet ();
SetContentView (Resource.Layout.MainRootView);
...
}
在 Setup.cs 中,我定义了一个自定义 Presenter 来控制加载新 viewModel 时显示的 fragment :
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 内容替换为某个 fragment :
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;
}
...
}
fragment 代码如下所示:
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;
}
...
此 fragment 包含少量动态创建的自定义 subview ,每个 subview 都与一个单独的 View 模型相关,在其创建后立即设置。
public class SubSectionView : LinearLayout, IHubSectionView
{
private MySubViewModel _vm;
public void SetContentViewModel (MySubViewModel vm)
{
_vm = vm;
...
}
现在,如果可能的话,如何将此 subview 内的 TextView 的属性绑定(bind)到我之前设置的 viewModel 的属性?
非常感谢您的任何建议!
最佳答案
我希望能理解您的情况,我遇到过与您的问题完全相同的形式:
我有一个 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 是这里的关键。它绑定(bind)我们的属性并将其传递给指定的模板。在那里我们可以使用我们的 SubViewModel 的属性。
所以我们只需要在我们的主视图模型中定义MySubView:
public class MainViewModel : MvxViewModel
{
...
private SubViewModel _mySubView = new SubViewModel();
public SubViewModel MySubView
{
get { return _mySubView; }
set { _mySubView = value; RaisePropertyChanged(() => MySubView); }
}
...
}
希望对您有所帮助。
关于android - MvvmCross 安卓 : How to create a binding for a subview of a fragment to an arbitrary MvxViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803975/