无法在app.xaml中找到MergedDictionaries

时间:2012-08-26 14:15:36

标签: .net wpf xaml resourcedictionary mergeddictionaries

我创建了一个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"}

2 个答案:

答案 0 :(得分:3)

您可能忘记将App的引用添加到CoreSystemStyles需要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
-

答案 1 :(得分:0)

尝试更改此

    <ResourceDictionary Source="ButtonStyles.xaml" />

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