简体   繁体   English

C# App.xaml 和资源字典

[英]C# App.xaml and Resource Dictionaries

I'm trying to get my application (WPF) use a referenced assembly in my App.xaml using Pack URIs.我试图让我的应用程序 (WPF) 使用包 URI 在我的 App.xaml 中使用引用的程序集。 In my solution I have several projects.在我的解决方案中,我有几个项目。 In the base application, I just have my App.xaml.在基本应用程序中,我只有我的 App.xaml。 I would like this to use a referenced assembly.我希望它使用引用的程序集。

Call my base project with App.xaml Project 1 (in the solution).使用 App.xaml 项目 1(在解决方案中)调用我的基础项目。 This is the relevant part of App.xaml:这是 App.xaml 的相关部分:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MyProject.Project2;component/Themes/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Project 2 has a folder "Themes" with Generic.xaml.项目 2 有一个带有 Generic.xaml 的文件夹“Themes”。 I'm fairly sure that I'm doing the Pack URI incorrectly because none of the styling in Generic.xaml is being used (I'm overriding the default window with a custom window, but it does not currently work. Application shows up with default window styling, everything inside the window is black).我很确定我没有正确地执行 Pack URI,因为没有使用 Generic.xaml 中的任何样式(我使用自定义窗口覆盖默认窗口,但它当前不起作用。应用程序显示为默认窗口样式,窗口内的所有内容都是黑色的)。

Folder hierarchy:文件夹层次结构:

MainFolder
    Project 1
        App.xaml
        bin
            Debug
                Application.exe
    Project 2
        Themes
            Generic.xaml

Questions:问题:

  1. Can someone explain the pack://application:,,, syntax?有人可以解释一下pack://application:,,,语法吗? What does all of this mean?这一切意味着什么?
  2. What am I doing wrong with the Pack URI? Pack URI 有什么问题?

Thanks in advance.提前致谢。

The first item in a Pack Uri after the ,,, is the name of the assembly. Pack Uri 中 ,,, 之后的第一项是程序集的名称。 Make sure you set that:确保您设置:

"pack://application:,,,/[ASSEMBLY name here];component/Themes/Generic.xaml"

尝试将资源字典移动到项目 2 而不是 App.xaml 中。

Figured it out.弄清楚了。 Turns out in MyProject.Project2 I needed the following code in AssemblyInfo.cs:结果在 MyProject.Project2 中,我需要在 AssemblyInfo.cs 中添加以下代码:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)
)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM