简体   繁体   中英

Cannot find resource dictionary in WPF application

I'm in the process of building a WPF application and ran into an error when trying to reference a resource dictionary. Inside my WPF application project I have a folder called "Styles" to contain all the xaml style templates for my app:

VS2010 Solution Explorer

In my app.xaml file I have this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/MetroTheme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

However, when I hover over the source property I get an error saying "An error occurred while finding the resource dictionary "Styles/MetroTheme.xaml". I can see the xaml file inside the folder both in Visual Studio and in the file system.

I have also tried "/Styles/MetroTheme.xaml" and a pack uri for the source property, both with no success. Any idea why I'm getting this file not found error?

Make sure that the build action for MetroTheme.xaml is set to Page.

在此输入图像描述

I had the same issue, but setting Build Action = Page did not solve for me. Turned out I needed to use the Pack URI Format . So,

<ResourceDictionary Source="pack://application:,,,/Styles/MetroTheme.xaml"/>

EDIT

Turns out the above will eliminate build error but still results in runtime error. I needed to include full assembly specification for complete resolution (even though all files are in same assembly):

<ResourceDictionary Source="pack://application:,,,/WpfApplication10;component/Styles/MetroTheme.xaml"/>

有时关闭Visual Studio并再次打开它可以解决这个问题。

将目标.net版本更改为项目属性中较旧的版本,然后重置为以前的版本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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