WPF MVVM:如何“按需"加载视图不使用插件架构? [英] WPF MVVM: How to load views "on demand" without using plug-in architecture?

查看:29
本文介绍了WPF MVVM:如何“按需"加载视图不使用插件架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似 Outlook 的 WPF UI,左侧有导航,顶部有一个工具栏,底部有一个状态栏,所有这些都在 DockPanel 中.

I have an Outlook-like WPF UI with navigation on the left, a toolbar on top, and a status bar at the bottom, all within a DockPanel.

主要"应用程序的区域在一个 DataGrid 中,它是 LastChild,因此填充了剩余的空间,它目前看起来像这样:

The "main" area of the application is within a DataGrid wich is the LastChild and therefore fills the remaining space and it currently looks like this:

<Grid DockPanel.Dock="Right">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <views:View1 Grid.Column="0" Grid.Row="0"/>
    <views:View2 Grid.Column="0" Grid.Row="0"/>
    <views:View3 Grid.Column="0" Grid.Row="0"/>
    <views:View4 Grid.Column="0" Grid.Row="0"/>
</Grid>

这些视图都是用户控件,它们拥有自己的 ViewModel,其中 Visibility 属性绑定到其 ViewModel 中的一个属性并由导航窗格选择控制.When a navigation pane item is selected the main ViewModel sends a message to ViewModels of the views and upon receiving the message the ViewModels of the views set their Visibility properties accordingly...

These views are all user controls that have their own ViewModels with Visibility property bound to a property in its ViewModel and controlled by the navigation pane selections. When a navigation pane item is selected the main ViewModel sends a message to ViewModels of the views and upon receiving the message the ViewModels of the views set their Visibility properties accordingly...

现在,这工作正常,但感觉不对,因为这些视图的所有 ViewModel 都在应用程序启动时实例化,而不是在相关导航窗格被选择时提出有关性能和内存使用的问题......

Now, this works fine but doesn't feel right since all the ViewModels of these views are being instantiated on the app start instead of when the related navigation pane is selected bringing up questions about performance and memory usage...

我的问题是,是否有一种方法可以在选择相关导航窗格时按需"加载每个视图及其 ViewModel,并在选择不同的导航窗格时卸载这些视图和 ViewModel,而无需求助于 PRISM/MEF或其他一些插件架构......而且,更一般地说,您将如何构建具有多个视图/视图模型嵌入"的应用程序?在主视图中?

My question is if there is a way of loading each view and its ViewModel "on demand", when the related navigation pane is selected and unloading these views and ViewModels when a different navigation pane is selected without resorting to PRISM/MEF or some other plugin architecture... and, more general how would you approach building an application like this with multiple views/ViewModels "embedded" within the main view?

推荐答案

这是我的工作:

首先,在您的主 ViewModel(服务于主窗口的那个)上,添加一个 Content 属性:

First, on your main ViewModel (the one that's servicing the main window), add a Content property:

object _content;
public object Content
{
    get { return _content; }
    set
    {
        _content = value;
        RaisePropertyChanged("Content");
    }
}

当用户从一个视图切换到另一个视图时,将 Content 属性设置为他们选择的视图的相应 ViewModel.

When the user switches from view to view, set the Content property to the appropriate ViewModel for that view they've selected.

然后,在主窗口中,您可以使用 ContentControl 显示当前的 Content ViewModel:

In the main window, then, you can display the current Content ViewModel using a ContentControl:

<ContentControl Content="{Binding Content}" />

... 以及上面的某处,为 Content 可能设置为的各种 ViewModel 中的每一个定义一个 DataTemplate:

... and somewhere above that, define a DataTemplate for each of the various ViewModels that Content might be set to:

<DataTemplate DataType="{x:Type vm:FooViewModel}">
    <my:FooUserControl />
</DataTemplate>

<DataTemplate DataType="{x:Type vm:BarViewModel}">
    <my:BarUserControl />
</DataTemplate>

所以现在 ContentControl 将负责初始化和显示您的每个 UserControl,具体取决于当前活动"的 ViewModel.

So now the ContentControl will be responsible for initializing and displaying each of your UserControls depending on which ViewModel is currently "active".

这篇关于WPF MVVM:如何“按需"加载视图不使用插件架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆