为什么在DataTemplate中使用UserControl比直接xaml慢? [英] Why using UserControl inside DataTemplate is slower than direct xaml?

查看:154
本文介绍了为什么在DataTemplate中使用UserControl比直接xaml慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:MyViewModel}">

           <!-- xaml is typed here directly -->
           <Border>
               ...
           </Border>

        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

DataTemplate内部的xaml很大(超过200行)。

And xaml inside the DataTemplate is big (more than 200 lines).

我想将DataTemplate内部的xaml移到单独的UserControl中,以使其易于编辑和维护。接下来,我要做:

I want to move xaml which is inside the DataTemplate into a separate UserControl to make it easier to edit and maintain. I do next:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:MyViewModel}">

            <!-- xaml is moved to separate UserControl -->
            <local:MyViewModelUserControl />

        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我遇到的问题是渲染/处理第二个代码(使用UserControl)大约需要2倍的时间比第一个代码长。有什么想法如何处理吗?

The issue I run into is that rendering/processing the second code (with UserControl) takes about 2 times longer than the 1st code. Any ideas how to deal with it?

注意:我不是移动ListBox,而是移动DataTemplate内部的xaml。原因不是重复使用此代码,而是最小化放置ListBox的主文件。另一件事是,我在ListBox内有几个DataTemplates(用于多个ViewModels),而xaml确实很大。这就是为什么我要将这个xaml(位于DataTemplate内部)移动到单独的控件中的原因。

NOTE: I'm moving not the ListBox, but the xaml which is inside the DataTemplate. The reason is not to reuse this code, but to minimize the main file where the ListBox is placed. Other thing is that I have several DataTemplates inside the ListBox (for several ViewModels) and the xaml is really huge. That's why I want to move this xaml (which is inside the DataTemplate) to a separate control.

推荐答案

我知道这是一个这个老问题,但最近我也遇到了这个问题。在WPF中创建用户控件的开销很大,这似乎是由于将代码隐藏类文件连接到XAML而引起的。如果您要做的只是将XAML移到另一个位置,只需在另一个文件的 ResourceDictionary 中定义您的 DataTemplate ,并将其加载为 StaticResource 。这将提供一些优点:

I know this is an old question, but I ran into this issue recently as well. There is significant overhead in creating user controls in WPF which seems to come from connecting a code-behind class file to the XAML. If all you are trying to do is move XAML to another location, simply define your DataTemplate in a ResourceDictionary in another file, and load it as a StaticResource. This will provide a few advantages:

(1)能够对元素使用 x:Name ,但不能内联 DataTemplate 允许。

(1) Ability to use x:Name for elements, which is not allowed in an inline DataTemplate.

(2)性能。具有直接XAML的 DataTemplate DataTemplate 中的 UserControl 快几个数量级。 code>。

(2) Performance. A DataTemplate with direct XAML is orders of magnitude faster than a UserControl in a DataTemplate.

(3)清洁度。您可以在任意位置定义 DataTemplate (同一文件中的资源字典,使用位置附近,其他文件等)并将其引用为一个 StaticResource

(3) Cleanliness. You can define the DataTemplate wherever you like (a resource dictionary in the same file, near where you're using it, a different file, etc.) and refer to it as a StaticResource.

这篇关于为什么在DataTemplate中使用UserControl比直接xaml慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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