将 MvxDialogFragment 与 ViewModel 绑定
Binding MvxDialogFragment with ViewModel
我有用于查看此 VM 的 ViewModel 和 DialogFragment。
在核心中,我以这种方式显示 VM
this.ShowViewModel<AnnotationEditViewModel>();
我在 AnnotationEditDialogFragment.cs
中有此代码
[Register("AnnotationEditDialogFragment")]
public class AnnotationEditDialogFragment : MvxDialogFragment<AnnotationEditViewModel>
{
...
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.EnsureBindingContextSet(savedInstanceState);
var view = this.BindingInflate(Resource.Layout.text_annotation_dialog_fragment, null);
var titleAnnotationTextView = view.FindViewById<TextViewWithFont>(Resource.Id.titleAnnotationTextView);
var set = this.CreateBindingSet<AnnotationEditFragment, AnnotationEditViewModel>();
set.Bind(this).For(be => be.CanBeSaved).To(vm => vm.CanBeSaved);
set.Apply();
builder = new AlertDialog.Builder(Activity);
LayoutInflater inflater = Activity.LayoutInflater;
builder.SetView(view);
alertDialog = builder.Create();
return alertDialog;
}
...
}
我在 Showv 方法的 MainPresenter 中有请求,我可以在其中检测到此请求。
如果我在 MainPresenter 中键入此代码:
var textAnnotationDialogFragment = Activity.FragmentManager.FindFragmentByTag(nameof(AnnotationEditFragment)) as AnnotationEditFragment ?? new AnnotationEditFragment();
textAnnotationDialogFragment.Show(Activity.FragmentManager, nameof(AnnotationEditFragment));
- 我看到对话框,但我没有与 VM 绑定/
如果我为 ShowViewModel() 键入此代码:
base.Show(request, fragmentRequest);
- 我收到错误消息,未找到任何 activity 或我的 VM 的片段/
如何与绑定到 VM 进行对话?
问题:
1. Presenter 处理的请求方法 - ViewModel Null
使用这种方法,AnnotationEditFragment
的 ViewModel
属性 为空,因为片段上没有设置 ViewModel
,因为它是在正常生命周期之外创建的mvx 在创建普通MvxFragments
时使用。 MvxDialogFragment
遵循不同的生命周期。
2。 Presenter 未处理的请求方法 - 否 Activity
使用这种方法,标准 Mvx 片段生命周期试图在 MvxDialogFragment
上执行。这需要指定一个 Activity 来放置片段。这不是我们想要的 MvxDialogFragment
.
方法
解法:
我相信方法 1 最接近于让我们成功实施工作 MvxDialogFragment
。我们需要做的就是确保 ViewModel 贯穿其生命周期。因此,在您的 MainPresenter
中,确保根据请求创建 AnnotationEditViewModel
的实例并将其分配给 AnnotationEditFragment
:
protected override void ShowActivity(MvxViewModelRequest request, MvxViewModelRequest fragmentRequest = null)
{
if (request.ViewModelType == typeof(NamesViewModel))
{
var dialog = new AnnotationEditFragment();
var viewModel = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, null) as AnnotationEditViewModel;
dialog.ViewModel = viewModel;
dialog.Show(Activity.FragmentManager, nameof(AnnotationEditFragment));
return;
}
base.ShowActivity(request, fragmentRequest);
}
IMvxViewModelLoader
用于确保ViewModel根据Mvx ViewModel生命周期标准加载,CIRS:
- Construction - using IoC for Dependency Injection
- Init() - initialisation of navigation parameters
- ReloadState() - rehydration after tombstoning
- Start() - called when initialisation and rehydration are complete
我有用于查看此 VM 的 ViewModel 和 DialogFragment。 在核心中,我以这种方式显示 VM
this.ShowViewModel<AnnotationEditViewModel>();
我在 AnnotationEditDialogFragment.cs
中有此代码[Register("AnnotationEditDialogFragment")]
public class AnnotationEditDialogFragment : MvxDialogFragment<AnnotationEditViewModel>
{
...
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.EnsureBindingContextSet(savedInstanceState);
var view = this.BindingInflate(Resource.Layout.text_annotation_dialog_fragment, null);
var titleAnnotationTextView = view.FindViewById<TextViewWithFont>(Resource.Id.titleAnnotationTextView);
var set = this.CreateBindingSet<AnnotationEditFragment, AnnotationEditViewModel>();
set.Bind(this).For(be => be.CanBeSaved).To(vm => vm.CanBeSaved);
set.Apply();
builder = new AlertDialog.Builder(Activity);
LayoutInflater inflater = Activity.LayoutInflater;
builder.SetView(view);
alertDialog = builder.Create();
return alertDialog;
}
...
}
我在 Showv 方法的 MainPresenter 中有请求,我可以在其中检测到此请求。
如果我在 MainPresenter 中键入此代码:
var textAnnotationDialogFragment = Activity.FragmentManager.FindFragmentByTag(nameof(AnnotationEditFragment)) as AnnotationEditFragment ?? new AnnotationEditFragment();
textAnnotationDialogFragment.Show(Activity.FragmentManager, nameof(AnnotationEditFragment));
- 我看到对话框,但我没有与 VM 绑定/
如果我为 ShowViewModel() 键入此代码:
base.Show(request, fragmentRequest);
- 我收到错误消息,未找到任何 activity 或我的 VM 的片段/ 如何与绑定到 VM 进行对话?
问题:
1. Presenter 处理的请求方法 - ViewModel Null
使用这种方法,AnnotationEditFragment
的 ViewModel
属性 为空,因为片段上没有设置 ViewModel
,因为它是在正常生命周期之外创建的mvx 在创建普通MvxFragments
时使用。 MvxDialogFragment
遵循不同的生命周期。
2。 Presenter 未处理的请求方法 - 否 Activity
使用这种方法,标准 Mvx 片段生命周期试图在 MvxDialogFragment
上执行。这需要指定一个 Activity 来放置片段。这不是我们想要的 MvxDialogFragment
.
解法:
我相信方法 1 最接近于让我们成功实施工作 MvxDialogFragment
。我们需要做的就是确保 ViewModel 贯穿其生命周期。因此,在您的 MainPresenter
中,确保根据请求创建 AnnotationEditViewModel
的实例并将其分配给 AnnotationEditFragment
:
protected override void ShowActivity(MvxViewModelRequest request, MvxViewModelRequest fragmentRequest = null)
{
if (request.ViewModelType == typeof(NamesViewModel))
{
var dialog = new AnnotationEditFragment();
var viewModel = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, null) as AnnotationEditViewModel;
dialog.ViewModel = viewModel;
dialog.Show(Activity.FragmentManager, nameof(AnnotationEditFragment));
return;
}
base.ShowActivity(request, fragmentRequest);
}
IMvxViewModelLoader
用于确保ViewModel根据Mvx ViewModel生命周期标准加载,CIRS:
- Construction - using IoC for Dependency Injection
- Init() - initialisation of navigation parameters
- ReloadState() - rehydration after tombstoning
- Start() - called when initialisation and rehydration are complete