c# - C# WPF项目正确添加样式资源文件

标签 c# .net wpf xaml

我已经阅读了很多关于 ResourceDictionaries 的内容,但我不确定它们与文件系统的关系。 如果我引用一个 ResourceDictionary Source="/X/a.b",这是否意味着我可以将资源文件 a.b 放在目录 X 下,其中 X 与我的 bin, obj, App.xaml等?

我正在尝试使用 Selen.Wpf.SystemStyles 做这样的事情,但我收到了 FileNotFound 错误。

The assembly with display name 'Selen.Wpf.SystemStyles' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Selen.Wpf.SystemStyles, Culture=neutral' or one of its dependencies. The system cannot find the file specified.

我已将所有资源放入目录中,并已确认这些文件符合我认为的文件系统预期。 我是否遗漏了其他约定?

导入资源的代码为:

    <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
            Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

最佳答案

您的代码尝试从名为 Selen.Wpf.SystemStyles 的外部程序集加载资源文件。里面有一个名为 Styles.xaml 的嵌入式资源文件。

从异常来看,您似乎缺少对提到的程序集的引用,或者它没有被复制到 bin\Debug 目录。

同时读入 MSDN 上的包 URI .

关于c# - C# WPF项目正确添加样式资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249554/

相关文章:

c# - 将简单的 ForEach 转换为 Linq 语句

c# - 如何在 ConfigureServices .Net Core 3.1 中注册具有多个接口(interface)的服务并将该服务用作单个实例

c# - 如何用几行代码产生 StackOverflowException?

c# - 设计时用户控件中的集合编辑器

c# - 使用 Message.Attach 时 Caliburn.Micro 和/或 WindowsBase 中出现 NullReferenceException

c# - RoutedEventHandler 未正确添加到指定元素

WPF 添加样式资源而无需在后面的代码中输入

c# - 具有多个数据库调用的方法的线程安全

.net - 除了必须总是自己编写之外,如何确保我们的函数在 F# 中使用缩写类型而不是基元?

c# - 承担多种角色的自定义 CodeAccessSecurityAttribute