如何创建资源别名?

时间:2014-02-02 17:57:54

标签: wpf xaml

此问题之前已被问过,但答案无效。根据我在这里读到的内容,这应该可行。

<ResourceDictionary>
      <LinearGradientBrush x:Key="emerald" EndPoint="1,0.5" StartPoint="0,0.5">
        <GradientStop Color="#FF00FD8A" Offset="0"/>
        <GradientStop Color="#FF4DBD65" Offset="1"/>
      </LinearGradientBrush>
      <StaticResourceExtension x:Key="MainPanelBackground" ResourceKey="emerald"/>

    </ResourceDictionary>

接着是

 <Frame x:Name="_mainFrame" Background="{StaticResource MainPanelBackground}" />

但是,我得到的是XAML预览器抱怨

  An object of the type "System.Windows.StaticResourceExtension" cannot be 
  applied to a property that expects the type "System.Windows.Media.Brush". 

运行程序只会产生XAML解析错误。

作为参考,这是另一个声称有答案的问题,但这对我不起作用,我无法弄清楚原因: Redefine/alias a resource in WPF?

1 个答案:

答案 0 :(得分:2)

使用<DynamicResourceExtension ...代替<StaticResourceExtension ...?在全局资源字典中使用StaticResourceExtension方法创建别名似乎存在问题,尽管我无法解释原因。