使用 ContentControl 生成视图 (UserControls) [英] Generating Views (UserControls) using ContentControl

查看:30
本文介绍了使用 ContentControl 生成视图 (UserControls)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl (MainView),它需要在其内部显示另一个 UserControl.根据某些条件,它将显示 AView 或 BView(它们都出现在 MainView 的同一位置).我使用的是 ViewModel 第一种方法,因此视图是通过数据模板生成的:

I have a UserControl (MainView) which needs to display another UserControl inside itself. Depending on some condition, it will display either AView or BView (they both appear on the same place in MainView). I am using ViewModel first approach, so Views are generated through data templates:

public class AView : UserControl { }
public class BView : UserControl { }
public class AViewModel : ViewModelBase { }
public class BViewModel : ViewModelBase { }

从资源使用的角度来看,这两种方式是否有区别:

From the resource usage point, is there a difference between these two approaches:

1) 拥有一个 ContentControl

1) Having one ContentControl

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

private ViewModelBase _someViewModel;
public ViewModelBase SomeViewModel
{
    get {return _someViewModel;}
    set
    {
        if (!ReferenceEquals(_someViewModel, value))
        {
            _someViewModel = value;
            RaisePropertyChange(SomeViewModel);
        }
    }
}

这样我就可以选择将哪个 ViewModel(AViewModel 或 BViewModel)设置为 SomeViewModel,DataTemplates 将选择合适的视图来显示.

This way I can choose which ViewModel (AViewModel or BViewModel) will I set to SomeViewModel, and DataTemplates will choose the appropriate view to display.

2) 放置两个 ContentControl,并控制每个的 Visibility(一次只能看到一个).

2) Placing two ContentControls, and control Visibility of each of them (only one is visible at a time).

<ContentControl Content="{Binding AViewModel}"
                Visibility="{Binding SomeCondition}" />

<ContentControl Content="{Binding BViewModel}"
                Visibility="{Binding NotSomeCondition}" />

那么,从资源管理的角度来看,这两个视图之间的切换行为会有所不同,还是在这两种情况下,在给定时间只有一个视图驻留在内存中?

So, from the point of resource management, will switching between these two views behave any differently, or in both cases only one view will reside in memory at a given time?

推荐答案

WPF 卸载不可见的对象,因此在任何给定时间,这两种方法都只会加载一个视图,但是您的第二个方法将在UI,而第一个只创建一个.

WPF unloads objects which are not visible so at any given time only one of your Views will be loaded for both methods, however your 2nd method will create two ContentControl's in the UI, while the first only creates one.

此外,每当您的内容视图模型发生变化时,评估Visibility 的开销都会增加(微乎其微).DataTemplate 将以任何一种方式进行评估,因为您将 Content 设置为 ViewModel,并且 WPF 将必须确定如何绘制该 ViewModel寻找 DataTemplate

In addition, there is the added (miniscule) overhead of evaluating the Visibility anytime your Content view model changes. The DataTemplate will be evaluated either way since you are setting the Content to a ViewModel, and WPF will have to determine how to draw that ViewModel by looking for the DataTemplate

我个人更喜欢第一个版本.更容易维护和管理,尤其是当你有不止 2 个视图,并且 UI 中一次只存在一个 ContentControl

Personally I prefer the first version. It's easier to maintain and manage, especially when you have more than just 2 views, and only one ContentControl exists in the UI at a time

这篇关于使用 ContentControl 生成视图 (UserControls)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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