【问题标题】:How to access the resource via key defined in generic.xaml如何通过 generic.xaml 中定义的键访问资源
【发布时间】:2013-03-14 09:55:29
【问题描述】:

当前我正在使用Generic.xaml 并在其中定义了一个自定义类实例。如下:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/TextPicker;component/Themes/FiltersTreeView.xaml" />
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

FiltersTreeView.xaml 中,我定义了一个自定义 UI 类的实例。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:utils="clr-namespace:TextPicker.Utils"
                    xmlns:cmd="clr-namespace:TextPicker.Commands"
                    xmlns:cc="clr-namespace:TextPicker.CustomControls"
                    xmlns:dm="clr-namespace:TextPicker.Models">

    <cc:ColorPiePopup x:Key="colorPiePopup"/>
    ....
<ResourceDictionary/>

现在,我尝试使用Application.Current.FindResource 来查找它,但它总是返回null

那么我怎样才能从Generic.xaml 中定义这个资源呢?我的意思是使用代码。

【问题讨论】:

  • 您是否在 App.xaml 中将 Generic.xaml 添加为 ResourceDictionary
  • AssemblyInfo.as 中有 ThemeInfo::ResourceDictionaryLocation.SourceAssembly,够吗?我检查了 App.xaml,目前 Generic.xaml 不在其中,但 Generic.xaml 确实有效,因为其中的其他样式设置适用于我的控件。

标签: wpf xaml resourcedictionary


【解决方案1】:

这个问题回答here:

您无法直接在应用中访问 generic.xaml 中的资源。 [...] 因此,如果您想使用 generic.xaml 中的资源,您应该将其作为通用 ResourceDictionary 添加到您的应用中

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2018-12-02
    • 2022-09-27
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多