【发布时间】: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 是否有任何链接?
谢谢
【问题讨论】: