回答を投稿していただきありがとうございます。それは100%正しいです、私はちょうど私自身の答えを投稿したい、私は他の誰かのために役立つことができる興味深いものを見つけたので。
答え:ResourceDictionaryインスタンスは、アプリケーションで参照されると(そのスタイルを使用するコントロールが多数であっても)一度だけ作成されますが、使用される別のResourceDictionaryで参照されるたびに再度インスタンス化されますアプリケーションで
それでは、私たちは次のような構造を持っているとしましょうあなたにこの場合の例を与える:
<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}">
<!-- Some setters -->
</Style>
CustomButtonResourceDictionary:ButtonResourceDictionary.xamlは、次のコードを持っている
- StylesAssembly.dll
- ButtonResourceDictionary.xaml
- CustomButtonResourceDictionary.xaml
- Application.exe
- App.xaml
- MainWindow.xaml
.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.exe
はStylesAssembly.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
は、1つのインスタンスのみを持つことになります。
私たちの
MainWindow.xamlはそれでこのような何かを持っている場合
しかし、CustomButtonResourceDictionary.xaml
1つのインスタンスを持っていますが、ButtonResourceDictionary.xaml
は2つのインスタンスています:
<StackPanel>
<Button Style="{StaticResource DefaultButtonStyle}" />
<Button Style="{StaticResource DefaultButtonStyle}" />
<Button Style="{StaticResource CustomButtonStyle}" />
<Button Style="{StaticResource CustomButtonStyle}" />
<Button Style="{StaticResource CustomButtonStyle}" />
</StackPanel>
それが起こるの最初の2 Buttons
使用しているため、スタイルDefaultButtonStyle
からButtonResourceDictionary.xaml
までですが、別の3つのButtons
の使用スタイルCustomButtonStyle
はから来て、ButtonResourceDictionary.xaml
をコードにマージします。
私はそれがテストするのはとても簡単だと思いますが、正確なシナリオを理解するのに問題があります。リソース辞書を持つ別のアセンブリを意味しますか? – Sinatr
@ mm8私が間違いを指摘してくれたことを分かりやすくするためにポイント1を編集しました –
@Sinatr問題は、 'ResourceDictionary'にコードがないので、私はそれをテストする方法がわかりません。そこにブレークポイントを置く。このシナリオは: 'StyleAssembly'は' ClassLibrary'であり、 'ButtonStyles'と呼ばれる' ResourceDictionary'を持ち、その中に 'CustomButtonStyle'を持っています。私のアプリケーションは 'StyleAssembly'を参照し、それをApp.xamlにマージし、私のMainWindowは' CustomButtonStyle'スタイルを使って20個の 'Buttons'を持っています。私のアプリケーションは 'ButtonStyles'インスタンスを20個作成しますか? –