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

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

问题描述

我有一个类似WPF UI的Outlook,其左侧导航,顶部工具栏和底部状态栏均在DockPanel之内.

I have an Outlook like WPF UI with navigation on the left, toolbar on top and 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中的属性,并由导航窗格的选择进行控制.选择导航窗格项后,主ViewModel会向视图的ViewModels发送一条消息,并且在收到该消息后,视图的ViewModels会相应地设置其Visibility属性...

These views are all user controls which 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 app start instead of when the related navigation pane is selected bringing up questions about performance and memory usage...

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

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(服务于主窗口的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天全站免登陆