屏幕上的 MvvmCross 导航

标签 mvvm xamarin mvvmcross

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/

    相关文章:

    android - 在 Xamarin.Android 中通过 MVVMCross 绑定(bind) OxyPlot

    c# - HttpClientHandler :Dispose (bool )' is inaccessible from method ` System.Net.ServicePointManager:CloseConnectionGroup(字符串)

    c# - 在 MVVMCross 中,是否可以关闭 View 模型并将值传递回导航堆栈中的前一个 View 模型?

    asp.net - 为 ViewModel 创建 View 帖子返回具有空外键属性的 ViewModel

    c# - 获取 ObservableCollection 中的项目类型

    xamarin - 在 Xamarin.Forms 布局中添加空间?

    ios - ContentView 的自定义渲染器 (iOS)

    android - 无法在双向模式下绑定(bind) MvxBindableListView

    c# - 如何在 TabControl 模板中使用自定义 TabItem?

    c# - 在WPF复选框列表中设置焦点