在单独的程序集中查找资源字典时出错 [英] Error finding resource dictionary in separate assembly

查看:30
本文介绍了在单独的程序集中查找资源字典时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序集(名为MyCompany"),用于从其他几个项目中引用,我想在其中放置通用类和资源.

I have an assembly (named "MyCompany") that I made for referencing from several other projects, where I want to put common classes and resources.

我有一个包含以下内容的 Recursos.xaml:

There I have a Recursos.xaml with the following:

<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:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    <ResourceDictionary Source="/Resources/Icons.xaml"/>
 </ResourceDictionary.MergedDictionaries>
<Style x:Key="estiloColumnaTitulo" TargetType="{x:Type GridViewColumnHeader}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Foreground" Value="#FF377099"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="#FF56A8E2" />
    <Setter Property="FontFamily" Value="Segoe UI Light" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Height" Value="25" />
</Style>
<Style x:Key="estiloFila" TargetType="{x:Type ListViewItem}">
    <Setter Property="BorderThickness" Value="0 0 0 1"/>
    <Setter Property="BorderBrush" Value="#FFB6D0E2"/>
</Style>
</ResourceDictionary>

按照我见过的另一个类似问题的建议,此文件编译操作设置为 Resource.

This file compile action is set to Resource as suggested in another similar questions that I have seen.

在我的项目中,除了添加了引用(类工作正常)之外,我还有以下内容:

In my project, besides having added the reference (classes are working fine), I have the follwing:

<Application x:Class="Inventarios.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">
 <Application.Resources>
    <ResourceDictionary Source="pack://application:,,,/MyCompany;component/Recursos.xaml"/>
 </Application.Resources>
</Application>

尝试查找资源字典pack://application:,,,/MyCompany;component/Recursos.xaml"时提示错误

It says error when trying to find resource dictionary 'pack://application:,,,/MyCompany;component/Recursos.xaml'

我也试过:

 <ResourceDictionary Source="/MyCompany;component/Recursos.xaml"/>

结果相同......我知道有很多问题都存在同样的问题,但没有一个给我一个有效的答案.

With same result... I know there are A LOT of questions with the same problem, but none of them gives me a working answer.

推荐答案

您有正确的 XAML...

You have the correct XAML...

<ResourceDictionary Source="pack://application:,,,/Isaval;component/Recursos.xaml"/>

... 假设您的程序集名为 Isaval,并且您的资源文件位于根目录中并名为 Recursos.xaml.

... assuming that your assembly is named Isaval and that your resource file is in the root directory and named Recursos.xaml.

来自 在 Windows Presentation Foundation 中打包 URI MSDN 上的页面:

From the Pack URIs in Windows Presentation Foundation page on MSDN:

资源文件包 URI - 引用程序集

编译成引用的资源文件的包URI程序集使用以下权限和路径:

The pack URI for a resource file that is compiled into a referenced assembly uses the following authority and path:

权限:application:///.

Authority: application:///.

路径:编译成一个资源文件的路径引用的程序集符合以下格式:

Path: The path for a resource file that is compiled into a referenced assembly conforms to the following format:

AssemblyShortName[;Version][;PublicKey];component/Path

AssemblyShortName 是引用程序集的简称.

AssemblyShortName is the short name for the referenced assembly.

;Version [可选] 指的是引用程序集的版本包含资源文件.这用于两个或多个加载具有相同短名称的引用程序集.

;Version [optional] refers to the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.

;PublicKey [可选] 指用于签名的公钥引用的程序集.这在两个或多个引用时使用加载具有相同短名称的程序集.

;PublicKey [optional] refers to the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.

;component 指定被引用的程序集是从本地程序集引用.

;component specifies that the assembly being referred to is referenced from the local assembly.

/Path 是资源文件的名称,包括其相对路径到引用程序集的项目文件夹的根目录.

/Path is the name of the resource file, including its path relative to the root of the referenced assembly's project folder.

以下示例显示 XAML 资源文件的包 URI位于引用程序集的项目文件夹的根目录中:

The following example shows the pack URI for a XAML resource file that is located in the root of the referenced assembly's project folder:

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

以下示例显示 XAML 资源文件的包 URI位于引用程序集项目文件夹的子文件夹中:

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder:

pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

以下示例显示 XAML 资源文件的包 URI位于引用的特定版本的根文件夹中程序集的项目文件夹:

The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder:

pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

关于构建操作,来自合并资源词典页面在 MSDN 上:

And regarding the Build Action, from the Merged Resource Dictionaries page on MSDN:

您的资源必须作为资源构建操作定义为项目的一部分.如果在项目中包含资源.xaml 文件作为Resource,则无需将该资源文件复制到输出目录,该资源已包含在已编译的应用程序中.您也可以使用内容构建操作,但您必须随后将文件复制到输出目录,并将资源文件以相同的路径关系部署到可执行文件.

Your resource must be defined as part of the project as a Resource build action. If you include a resource .xaml file in the project as Resource, you do not need to copy the resource file to the output directory, the resource is already included within the compiled application. You can also use Content build action, but you must then copy the files to the output directory and also deploy the resource files in the same path relationship to the executable.

这篇关于在单独的程序集中查找资源字典时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆