【问题标题】:Unable to find MergedDictionaries in app.xaml无法在 app.xaml 中找到 MergedDictionaries
【发布时间】:2012-08-26 14:15:36
【问题描述】:

我创建了一个 WPF 应用程序(名为“Ferhad.Wpf”)。然后我在解决方案中添加了两个名为“Ferhad.Wpf.Core”和“Ferhad.Wpf.SystemStyles”的类库。

“Ferhad.Wpf.Core”中只有“Resources.xaml”。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="Foreground" Color="#FFFFFF" />
    <SolidColorBrush x:Key="BackgroundNormal" Color="#3F3F46" />
    <SolidColorBrush x:Key="BorderBrushNormal" Color="#54545C" />
    <SolidColorBrush x:Key="BorderBrushHighlighted" Color="#6A6A75" />
</ResourceDictionary>

但“Ferhad.Wpf.SystemStyles”中也有“ButtonStyles.xaml”和“Styles.xaml”。 这是“ButtonStyles.xaml”:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.Core;component/Resources.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="StandartButton" TargetType="Button">
        <Setter Property="Visibility" Value="Visible" />
        <Setter Property="Foreground" Value="{StaticResource Foreground}" />
        <Setter Property="Background" Value="{StaticResource BackgroundNormal}" />
        <Setter Property="BorderBrush" Value="{StaticResource BorderBrushNormal}" />
    </Style>
</ResourceDictionary>

和“Styles.xaml”:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="Button" BasedOn="{StaticResource StandartButton}" />
</ResourceDictionary>

这里是 app.xaml:

<Application x:Class="Ferhad.Wpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.SystemStyles;component/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在 MainWindow.xaml 的设计时一切似乎都很好,但是在运行应用程序时抛出异常:Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

我用谷歌搜索过,但没有任何帮助。我曾尝试更改文件的 BuildAction,但这也无济于事。

更新:

例外是行号'8'和行位置'18'。

内部异常是:{"Could not load file or assembly 'Ferhad.Wpf.SystemStyles, Culture=neutral' or one of its dependencies. The system cannot find the file specified.":"Ferhad.Wpf.SystemStyles, Culture=neutral"}

【问题讨论】:

  • 你能把内部异常和行号贴在它指向的地方吗?哪个资源字典引发了异常
  • @LeoLuis 我有更新问题。感谢您提供帮助。

标签: .net wpf xaml resourcedictionary mergeddictionaries


【解决方案1】:

您可能忘记添加从AppCore 的引用,SystemStyles 需要Core,因此如果构建过程仅复制SystemStyles,则它没有Core 程序集。

App
-> SystemStyles
-> Core

SystemStyles
-> Core //Not strictly necessary because the build process does not copy the 
        //reference to the App project anyway and the resources
        //are not resolved at compile-time

Core
-

【讨论】:

  • 成功了。 :)) 我不知道我是怎么做到的? :)) 无论如何,真的非常感谢。我需要 2 个小时。
  • @FarhadJabiyev:你可以说这是构建过程中的一个错误,理论上只有App-&gt;SystemStylesSystemStyles-&gt;Corereferences应该是必需的......
【解决方案2】:

尝试改变这个

    <ResourceDictionary Source="ButtonStyles.xaml" />

  <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.SystemStyles;component/ButtonStyles.xaml" />

【讨论】:

  • 这不会改变任何东西。
  • 有时,资源会通过一些奇怪的方式找到引用并抛出无法加载的异常并将其更改为包 uri 源有效。就像 Image Source uris 明确说只是文件名可能有效,但很快它就会抛出异常并需要包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多