C#App.xaml和资源字典

时间:2012-11-14 15:56:40

标签: c# wpf

我正在尝试让我的应用程序(WPF)使用Pack URI在我的App.xaml中使用引用的程序集。在我的解决方案中,我有几个项目。在基础应用程序中,我只有我的App.xaml。我希望这个使用引用的程序集。

使用App.xaml Project 1(在解决方案中)调用我的基础项目。这是App.xaml的相关部分:

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

Project 2有一个带有Generic.xaml的文件夹“Themes”。我很确定我正在做错误的包URI,因为Generic.xaml中没有任何样式被使用(我用自定义窗口覆盖了默认窗口,但它当前不起作用。应用程序显示为默认窗口样式,窗口内的所有内容都是黑色的。)

文件夹层次结构:

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

问题:

  1. 有人可以解释pack://application:,,,语法吗?所有这些意味着什么?
  2. 我在使用Pack URI做错了什么?
  3. 提前致谢。

3 个答案:

答案 0 :(得分:1)

在,,,之后的Pack Uri中的第一项是程序集的名称。确保你设置:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

想出来。原来在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)
)]
相关问题