在我发布了一个星期前发布的问题之后,我使用MVVMCross的WPF应用程序,开始阅读有关演示者的工作方式。关于如何使用移动应用程序做某事的文档和视频很多,尤其是iOS,但对于桌面Windows WPF应用程序不多。根据n = 24视频,我创建了一个我从mvxsimplewpfviewpresenter派生的主持人,然后继续覆盖存在的函数(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>()); }
}
}
,我的第一视图看起来像这样(在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>
之后,我使我的演示者成为一个要使用的代替默认设备,并能够用红色背景显示视图,而从我的主视图中带有蓝色背景的视图,其中包含菜单。因此,这本质上可以很好地与我想做的事情一起使用。
所以我要去哪里?我想知道的是,这与我看到的所有其他示例都大不相同,这些示例使用了主持人的显示方法,您需要使用MVX。MVXSIMPLEWPFVIEWPRESENTER和MVXSIMPLEWPFFVIEWPRESENTER类都无法覆盖。通过调用命令,ShowViewModel呼叫演示者(我假定)以显示我的新视图,但是当我获得Frameworkelement时,我不必在此处呼吁解决。我试图理解背后的机制,以便更好地调试我应该有问题。.是否有与上一篇文章中提到的容器有任何链接?
谢谢
那么决心在哪里完成?
这是在基本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/cirrious.mvvmcross.wpf/views/mvxwpfvviewperpresenter.cs
Show
可能是此类中的virtual
- 但这是一个非常简单的类,我一直期望WPF用户只能编写自己的IMvxWpfViewPresenter
上一篇文章中提到的容器有任何链接?
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/cirrious.mvvmcross.wpf/views/mvxwpfvewpfvviewewscontainer.cs
它从https://github.com/mvvmcross/mvvmcross/blob/v3.1/cirrious/cirrious/cirrious.mvvmcross/views/mvxviews/mvxviewscontainer.cs-
在设置过程中,容器中使用反射填充Type
s-请参见https://github.com/mvvmcross/mvvmcross/mvvmcross/wiki/customising-customising-uspemising-uspomis-set-----------------------------wik---wik--------------参见 InitalizeViewLookup
信息。-ViewModel-associations