gpt4 book ai didi

屏幕上的 MvvmCross 导航

转载 作者:行者123 更新时间:2023-12-03 10:59:44 29 4
gpt4 key购买 nike

screen

我们的设计师创建了一个类似于上面屏幕的布局。主要思想是创建一个只有一个屏幕的应用程序,当您点击一个按钮时,屏幕的红色部分会发生变化(即 2 个文本框而不是 1 个文本框)。这个应用程序将是一个多平台应用程序,我正在使用 MvvmCross 来创建它。我的问题是我怎样才能在 Mvvm 中实现这种行为?我的第一个想法是sg。像下面的代码,但我对这个解决方案不满意。你有什么更好的解决这个问题的办法吗?我应该以某种方式覆盖 ShowViewModel() 上的默认导航吗?

public class MainViewModel : MvxViewModel
{
private MvxViewModel _currentViewModel;
public MvxViewModel CurrentViewModel
{
get { return _currentViewModel; }
set { _currentViewModel = value; RaisePropertyChanged(() => CurrentViewModel); }
}

public MainViewModel()
{
CurrentViewModel = new DefaultViewModel();
}

public void OnButtonClick()
{
CurrentViewModel = new SecondViewModel();
}
}

public partial class MainViewModel : MvxViewController
{

public override void ViewDidLoad ()
{
base.ViewDidLoad ();
FirstViewModel.WeakSubscribe(ViewModelPropertyChanged);
}

private void ViewModelPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == "CurrentViewModel")
{
if (Model.CurrentViewModel != null)
{
if (Model.CurrentViewModel is SecondViewModel)
{
//remove bindings
//change View
//bind new viewmodel
}
}
}
}

最佳答案

这种“非页面导航”的替代方法与 MvvmCross Dialog 中的类似。 :

你可以:

  • 定制MvxPresenter允许使用 ShowViewModel
  • 在 Core 项目中放置一个特殊的接口(interface),并使用 Inversion of Control 将 UI 项目中的实现注入(inject)到 Core 项目中
  • 使用 MvxMessenger 插件并在触发此类导航的 Core 和 UI 项目之间共享消息。
  • 在 ViewModel 上使用具有特殊接口(interface)的属性(如 IInteractionRequest ) - 当 UI 需要更改时,该属性将触发事件。

  • 就个人而言,对于您的情况,我非常喜欢这些选项中的第一个 - 拦截 ShowViewModel使用演示者。

    我可能会考虑的另一种选择是使用某种“适配器驱动”控件,它可以很容易地根据 CurrentViewModel 更新它的子内容。属性(property)。在 Android 上,这就像使用 MvxLinearLayout 一样简单。带适配器。然而,在 iOS 上,我认为你必须编写一些新的东西才能做到这一点——因为 iOS 并没有真正的 LinearLayout/StackPanel 控件。

    关于屏幕上的 MvvmCross 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735497/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com