【问题标题】:C# App.xaml and Resource DictionariesC# App.xaml 和资源字典
【发布时间】:2012-11-14 15:56:39
【问题描述】:

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

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

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

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

文件夹层次结构:

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

问题:

  1. 有人能解释一下pack://application:,,, 的语法吗?这一切意味着什么?
  2. Pack URI 有什么问题?

提前致谢。

【问题讨论】:

标签: c# wpf


【解决方案1】:

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

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

【讨论】:

  • 是的,程序集名称设置正确。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    想通了。结果在 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)
    )]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多