【问题标题】:Following a recent post I made and was suggested to use A PRESENTER在我最近发表的一篇文章之后,有人建议我使用 A PRESENTER
【发布时间】:2014-03-05 20:59:50
【问题描述】:

在我大约一周前发布的一个问题WPF application using MVVMCROSS 之后,我开始阅读有关演示者如何工作的内容。有很多关于如何使用移动应用程序做某事的文档和视频,尤其是 IOS,但对于 DESKTOP Windows WPF 应用程序来说却不是很多。基于 N=24 视频,我创建了一个从 MvxSimpleWpfViewPresenter 派生的演示者并继续覆盖 函数 Present(System.Windows.FrameworkElement frameworkElement),主窗口将显示我的主视图和我将调用的所有其他视图,将在我的主视图下显示其内容:

  public class MyPresenter : Cirrious.MvvmCross.Wpf.Views.MvxSimpleWpfViewPresenter
  {
    private Window _mainWindow = null;
    private MvxWpfView _firstView = null;

    public MyPresenter(Window mainWindow)
      : base(mainWindow)
    {
      _mainWindow = mainWindow;
    }

    public override void Present(System.Windows.FrameworkElement frameworkElement)
    {
      //_mainWindow.DisplayGrid

      if(_firstView == null &&
         frameworkElement is FirstView)
      {
        _firstView = frameworkElement as FirstView;
        _mainWindow.Content = _firstView;
      }
      else if(_firstView != null)
      {
        if ((_firstView as FirstView).DisplayGrid.Children.Count > 0)
        {
          (_firstView as FirstView).DisplayGrid.Children.RemoveAt(0);
        }

        (_firstView as FirstView).DisplayGrid.Children.Add(frameworkElement);
      }
    }

我的主视图(称为 FirstViewModel)如下所示:

  public class FirstViewModel : MvxViewModel
  {
    public ICommand BlueCommand
    {
      get { return new MvxCommand(() => ShowViewModel<BlueViewModel>()); }
    }

    public ICommand RedCommand
    {
      get { return new MvxCommand(() => ShowViewModel<RedViewModel>()); }
    }
  }

我的 FirstView 看起来像这样(在 Xaml 中):

    <views:MvxWpfView 
             x:Class="WpfApplication1.Views.FirstView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:views="clr-namespace:Cirrious.MvvmCross.Wpf.Views;assembly=Cirrious.MvvmCross.Wpf"
             mc:Ignorable="d" Height="Auto" Width="Auto">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Name="RedCommandMenuItem" Header="Red command" Command="{Binding Path=RedCommand}" />
      <MenuItem Name="BlueCommandMenuItem" Header="Blue command" Command="{Binding Path=BlueCommand}" />
    </Menu>
    <Grid Name="DisplayGrid" Grid.Row="1">
    </Grid>
  </Grid>
</views:MvxWpfView>

之后,我将我的演示者设置为使用而不是默认设置,并且能够在包含菜单的主视图中显示具有红色背景的视图和具有蓝色背景的视图。所以这基本上可以很好地满足我想要做的事情。

那么我要去哪里呢?我想知道的是,这与我看到的所有其他示例非常不同,这些示例将使用演示者的 SHOW METHOD ,您需要一个模型,并且您需要使用 Mvx.Resolve 来创建视图。. Show 方法是不能同时被 MvxSimpleWpfViewPresenter 和 MvxSimpleWpfViewPresenter 类覆盖。通过调用命令,ShowViewModel 调用 Presenter(我假设)来显示我的新视图,但我不需要在这里调用 Resolve,因为我得到了 FrameworkElement.. 那么解析在哪里完成以及由谁完成?我正在尝试了解背后的机制,以便在遇到问题时更好地调试它。上一篇文章中提到的 CONTAINER 是否有任何链接?

谢谢

【问题讨论】:

    标签: wpf mvvm mvvmcross


    【解决方案1】:

    那么解决方案是在哪里完成的,由谁完成?

    这是在基础MvxWpfViewPresenter 中完成的

    public abstract class MvxWpfViewPresenter
        : IMvxWpfViewPresenter
    {
        public void Show(MvxViewModelRequest request)
        {
            try
            {
                var loader = Mvx.Resolve<IMvxSimpleWpfViewLoader>();
                var view = loader.CreateView(request);
                Present(view);
            }
            catch (Exception exception)
            {
                MvxTrace.Error("Error seen during navigation request to {0} - error {1}", request.ViewModelType.Name,
                               exception.ToLongString());
            }
        }
    
        public abstract void Present(FrameworkElement frameworkElement);
    
        public virtual void ChangePresentation(MvxPresentationHint hint)
        {
            MvxTrace.Warning("Hint ignored {0}", hint.GetType().Name);
        }
    }
    

    https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewPresenter.cs

    Show 在这个类中可能应该是 virtual - 但这是一个非常简单的类,我一直希望 WPF 用户只编写自己的 IMvxWpfViewPresenter 版本

    上一篇文章中提到的 CONTAINER 有链接吗?

    WPF 的默认容器是:

    public class MvxWpfViewsContainer
        : MvxViewsContainer
        , IMvxWpfViewsContainer
    {
        public FrameworkElement CreateView(MvxViewModelRequest request)
        {
            var viewType = GetViewType(request.ViewModelType);
            if (viewType == null)
                throw new MvxException("View Type not found for " + request.ViewModelType);
    
            // , request
            var viewObject = Activator.CreateInstance(viewType);
            if (viewObject == null)
                throw new MvxException("View not loaded for " + viewType);
    
            var wpfView = viewObject as IMvxWpfView;
            if (wpfView == null)
                throw new MvxException("Loaded View does not have IMvxWpfView interface " + viewType);
    
            var viewControl = viewObject as FrameworkElement;
            if (viewControl == null)
                throw new MvxException("Loaded View is not a FrameworkElement " + viewType);
    
            var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader>();
            wpfView.ViewModel = viewModelLoader.LoadViewModel(request, null);
    
            return viewControl;
        }
    }
    

    来自https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewsContainer.cs

    它继承自 https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross/Views/MvxViewsContainer.cs - 它在所有平台上共享。

    在设置期间,容器使用反射填充视图 Types - 请参阅 https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-overriding-view-viewmodel-associations 中的 InitalizeViewLookup 信息

    【讨论】:

    • 非常感谢您的回答和帮助。我所说的 LINK 并不是一个指向容器代码的链接,而是一个与容器的 CONNECTION 以及我之前所说的内容。抱歉,选词有误。
    • 容器是我的回答中基本演示者代码中使用的IMvxSimpleWpfViewLoader - 请参阅接口继承github.com/MvvmCross/MvvmCross/blob/…
    • 再次感谢!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多