【问题标题】:Referencing resource dictionaries from an external project从外部项目引用资源字典
【发布时间】:2019-10-11 15:40:50
【问题描述】:

是否可以在单独的库(框架)项目中制作资源字典,然后在另一个库项目中引用它?在这个单独的项目中,我有一个想要使用它的 UserControl。

这就是我的解决方案的设置方式。

SharedProject 有资源字典,我在我的插件项目中引用它。然后我将我的插件库导出到 MainProject 并让它显示我的插件视图。

我有所有这些工作......除了我的字典。我尝试在每个视图中引用我的字典

<UserControl.Resources>  
        <ResourceDictionary x:Key="dictionary">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/InternalShared;component/Resources/Dictionary1.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/InternalShared;component/Resources/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

但是当我尝试引用现有样式时...

<Button     Command="{Binding CommandOpenFile}"
            Style="{DynamicResource GenericButtonStyle}" 
            IsEnabled="{Binding IsEnabled}"
            Content="Restore Window" 
            Height="Auto" />

但我得到资源“GenericButtonStyle”无法解析。这种情况在我尝试引用任何字典中的样式的任何地方都会发生。我不知道为什么。

谁能告诉我为什么会这样?我还需要在其他地方设置此声明吗?

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    我维护一个被许多不同解决方案使用的外部 WPF UI 库。

    • 确保 InternalShared 是 WPF DLL
    • 改为将外部资源合并到主 WPF 应用的 App.xaml

    例如,使用我的资源的应用程序像这样合并它们:

    <ResourceDictionary Source="/InternalShared;component/Resources/Dictionary1.xaml"/>
    

    【讨论】:

    • 感谢您的帮助。我不知道在 App.xaml 中简单地设置你的字典就可以让所有项目都像这样简单地引用它们的元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多