ComponentResourceKey как проблема DynamicResource - PullRequest
3 голосов
/ 23 октября 2009

Я вижу неожиданное поведение при использовании ComponentResourceKey. Проблема возникла, потому что я использовал имена, используемые в классе SystemColors.

Рассмотрим следующий код:

Сначала я предоставляю ComponentResourceKey в качестве статического свойства:


public enum ColorKeys
{
    ControlColor
}

public class MyColors
{
    public static ComponentResourceKey ControlColorKey
    {
        get 
        { 
            return new ComponentResourceKey(typeof(MyColors), ColorKeys.ControlColor); 
        }
    }
}

Использование ключа в разметке для назначения ресурса этому ключу работает. Вызов этого ресурса как DynamicResource с разметкой ComponentResourceKey работает. Но вызов этого же ресурса с использованием статического свойства разрешается в ресурсе SystemColors.ControlColorKey.

Вот xaml:

<Window.Resources>
    <Color x:Key="{x:Static l:MyColors.ControlColorKey}">#FF0000FF</Color>
    <SolidColorBrush x:Key="UsingVerbose" 
                     Color="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type l:MyColors}, 
                                ResourceId={x:Static l:ColorKeys.ControlColor}}}"/>
    <SolidColorBrush x:Key="UsingStatic" 
                     Color="{DynamicResource {x:Static l:MyColors.ControlColorKey}}"/>                
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Height="20" Width="80" Background="{DynamicResource UsingVerbose}" Content="Verbose"/>
        <Button Height="20" Width="80" Background="{DynamicResource UsingStatic}" Content="Static"/>
    </StackPanel>
</Grid>

Когда отображается окно, кнопка «Подробно» отображает синий фон. Но нижняя кнопка «Статический» показывает цвет SystemColors.ControlColorKey.

Изменение имени статического свойства на другое имя приводит к тому, что обе кисти приобретают правильный цвет.

Но я не уверен, почему DynamicResource по-разному разрешает статический экземпляр ключа и экземпляр разметки?

Спасибо

К * * тысяче двадцать-одина

PS: Вот соответствующий вывод трассировки ResourceDictionarySource

System.Windows.ResourceDictionary Start: 10 : New resource dictionary set; Owner='TestMergedDictionaries.Window1'; Owner.HashCode='64923656'; Owner.Type='TestMergedDictionaries.Window1'; OldDictionary='0'; NewDictionary='System.Windows.ResourceDictionary'; NewDictionary.HashCode='44624228'; NewDictionary.Type='System.Windows.ResourceDictionary'
System.Windows.ResourceDictionary Stop: 10 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingVerbose'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='System.Windows.Controls.Button'; Element.HashCode='52727599'; Element.Type='System.Windows.Controls.Button'; ResourceKey='UsingVerbose'
System.Windows.ResourceDictionary Start: 3 : Found resource item on an element; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='UsingVerbose'; Value='System.Windows.DeferredResourceReference'; Value.HashCode='14347911'; Value.Type='System.Windows.DeferredResourceReference'
System.Windows.ResourceDictionary Stop: 3 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='System.Windows.Controls.Button'; Element.HashCode='52727599'; Element.Type='System.Windows.Controls.Button'; ResourceKey='UsingVerbose'

... removed uninteresting traces ...

System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'
System.Windows.ResourceDictionary Start: 3 : Found resource item on an element; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='System.Windows.DeferredResourceReference'; Value.HashCode='23488915'; Value.Type='System.Windows.DeferredResourceReference'
System.Windows.ResourceDictionary Stop: 3 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='23264094'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='System.Windows.Controls.TextBlock'; Value='DefAttributeKeyType typeId=TextBlock'; Value.HashCode='23521433'; Value.Type='System.Windows.Markup.BamlDefAttributeKeyTypeRecord'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='23264094'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='System.Windows.Controls.TextBlock'; Value='System.Windows.Style'; Value.HashCode='5'; Value.Type='System.Windows.Style'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'; Value='DefAttributeKeyString'; Value.HashCode='24854661'; Value.Type='System.Windows.Markup.BamlDefAttributeKeyStringRecord'
System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element=''; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Start: 8 : Found resource item in theme; Dictionary='System.Windows.SystemResourceHost'; Dictionary.HashCode='65273341'; Dictionary.Type='System.Windows.SystemResourceHost'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'; Value='#FFF0F0F0'
System.Windows.ResourceDictionary Stop: 8 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element=''; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'; Value='#FFF0F0F0'; Value.HashCode='12408161'; Value.Type='System.Windows.Media.SolidColorBrush'

System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Start: 8 : Found resource item in theme; Dictionary='System.Windows.SystemResourceHost'; Dictionary.HashCode='65273341'; Dictionary.Type='System.Windows.SystemResourceHost'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'; Value='#FFF0F0F0'
System.Windows.ResourceDictionary Stop: 8 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='KeyElementStart typeId=StaticExtension'; Value.HashCode='54371668'; Value.Type='System.Windows.Markup.BamlKeyElementStartRecord'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='#FF0000FF'

...