组件资源键作为动态资源的问题

3

我看到使用ComponentResourceKey时出现了一些意外行为。问题是因为我使用了SystemColors类中使用的名称。

请考虑以下代码:

首先,我将一个ComponentResourceKey公开为静态属性:


public enum ColorKeys
{
    ControlColor
}

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

在标记中使用键来分配资源是有效的。使用ComponentResourceKey标记将该资源作为DynamicResource调用也是有效的。但是,使用静态属性调用相同的资源会解析为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在键的静态实例和由标记实例化的实例中解析方式不同?
谢谢,
K
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'

1个回答

0
在你的静态ControlColorKey中,每次访问getter时都会创建一个新的ComponentResourceKey实例。因此第二次使用引用了不同的对象,没有得到更新的颜色。 如果你将代码更改为简单的静态字段,它将按预期工作:
public static ComponentResourceKey ControlColorKey = new ComponentResourceKey(typeof(MyColors), ColorKeys.ControlColor);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接