2017-10-18 212 views
2

我发现this post关于在网络上提高ResourceDictionaries的性能,但问题在于它很旧(2011)。我正在考虑实现这样的事情,但我想知道微软在.NET Framework的最新版本中是否没有解决这个问题。我有一个关于这个话题几个问题,其中,我希望这里有人能给出一个答案:同一个ResourceDictionary的多个实例

  1. 是.NET框架4.6.1仍然使他们的多个实例,一个是每次分配时间管理ResourceDictionaries控制?
  2. 它甚至有一个问题,当我在有例如风格"CustomButtonStyle"ResourceDictionary称为装配叫"StylesAssembly",然后在应用程序的App.xaml引用"ButtonStyles",已经20 ButtonsCustomButtonStyle分配给他们?
  3. 我是否正确理解,在上述情况下,将会出现"ButtonStyles"ResourceDictionary的20个实例?
+0

我想这是很容易测试,但我有问题,以了解确切的情况。你的意思是与资源字典或什么的另一个程序集? – Sinatr

+0

@ mm8我编辑了第1点,使其更容易理解,谢谢指出我的错误 –

+0

@Sinatr问题是我不知道如何测试它,因为'ResourceDictionary'没有后面的代码,所以我可以'在那里放置一个断点。这个场景是:'StyleAssembly'是一个'ClassLibrary',它有'ResourceDictionary'叫'ButtonStyles',它有'CustomButtonStyle'。我的应用引用'StyleAssembly'并将其合并到App.xaml中,并且我的MainWindow具有20个'Buttons'使用样式'CustomButtonStyle'。它会让我的应用程序创建20个'ButtonStyles'实例吗? –

回答

-1

谢谢@ mm8发布您的答案。这是100%正确的,我只想发布我自己的答案,因为我发现了一些有趣的东西,对其他人有用。

答案是:如果在应用程序中引用ResourceDictionary实例(无论许多控件使用它的样式),它只会被创建一次,但是它会在每次被另一个ResourceDictionary引用时再次实例化在应用程序中。


所以给你例子这种情况下,让我们说,我们有以下结构:

- StylesAssembly.dll 
    - ButtonResourceDictionary.xaml 
    - CustomButtonResourceDictionary.xaml 

- Application.exe 
    - App.xaml 
    - MainWindow.xaml 

ButtonResourceDictionary.xaml具有下面的代码:

<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}"> 
    <!-- Some setters --> 
</Style> 

CustomButtonResourceDictionary .xaml具有以下代码,它使用ButtonResourceDictionary.xaml

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="ButtonResourceDictionary.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultButtonStyle}"> 
    <!-- Some setters --> 
</Style> 

Application.exeStylesAssembly.dll的引用,并没有在App.xaml中一个下面的代码:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/StylesAssembly;component/ButtonResourceDictionary.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/StylesAssembly;component/CustomButtonResourceDictionary.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

现在,如果我们MainWindow.xaml有这样的事情在里面,ButtonResourceDictionary.xaml将只有一个实例

<StackPanel> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
</StackPanel> 

但如果我们的MainWindow.xaml有这样的事情在这里面,CustomButtonResourceDictionary.xaml将有一个实例,但ButtonResourceDictionary.xaml将有两个实例

<StackPanel> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource DefaultButtonStyle}" /> 
    <Button Style="{StaticResource CustomButtonStyle}" /> 
    <Button Style="{StaticResource CustomButtonStyle}" /> 
    <Button Style="{StaticResource CustomButtonStyle}" /> 
</StackPanel> 

这是因为前两个Buttons使用款式DefaultButtonStyleButtonResourceDictionary.xaml,但另外三种Buttons使用款式CustomButtonStyle来自CustomButtonResourceDictionary.xaml,它在其代码中合并ButtonResourceDictionary.xaml

+0

这是预期的行为。如何在不创建实例的情况下解决合并后的问题? – mm8

+0

我不会与它争论。这很难找到(从我今天经历的),所以我决定把所有东西都放在一个地方,然后放在这里。也许它会在未来帮助别人。 –

2

难道我理解正确的话,在上述情况下,会出现“ButtonStyles” ResourceDictionary的20个实例?

不,只有一个。

它甚至有一个问题,当我在集会名为“StylesAssembly”,然后在应用程序的App.xaml引用,有20个按键与CustomButtonStyle有例如式“CustomButtonStyle”在我ResourceDictionary称为“ButtonStyles”分配给他们?

如果合并ResourceDictionary到您的App.xaml和创建跨在你的应用程序的意见20个Button元素,仍然会有一个只创造了ResourceDictionary类的实例。

您可以通过添加一个代码隐藏类的ResourceDictionary证实了这一点自己:

Is it possible to set code behind a resource dictionary in WPF for event handling?

...并把断点在构造函数中。

在创建的ResourceDictionary中也只会有一个Style定义的实例。

+0

谢谢,我不知道我可以在类的后面添加一个代码到'ResourceDictionary'。我想自己检查一下,如果有效,我会将其标记为答案。 –

+0

@ mm8,以及声明*“每次控件引用ResourceDictionary XAML都会从[here](https://wpftutorial.net/MergedDictionaryPerformance.html)创建它的新实例”?我并不是说你错了,但我怀疑他们使用了另一种情况,而不是你测试过的。 – Sinatr

+0

问题是,这篇文章是从2011年。这就是为什么我问这个问题。它可能已过时 –

相关问题