0

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

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>

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>

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

I have also tried:

 <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.

Pinx0
  • 1,248
  • 17
  • 28
  • Assuming that _Isaval_ is Assembly Name from assembly Properties build action on the resource dictionary should be _Page_ (default) and that assembly must be referenced in your application – dkozl May 08 '15 at 15:22
  • The most important is does it work ok at runtime ? I guess it does and you probably hit an annoying xaml designer bug. I too got "errors" for xaml files opened in visual studio but it compiles just fine. If you close the xaml files then the errors vanish. Irritating but not fatal. – frenchone Sep 28 '22 at 10:43
  • @frenchone this is 7 years old, tbh I dont' even remember what the solution was. – Pinx0 Sep 28 '22 at 11:42
  • Seems to be fixed in VS 2022 now. Not in 2019 – frenchone Sep 29 '22 at 07:33

1 Answers1

1

You have the correct XAML...

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

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

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

Resource File Pack URIs - Referenced Assembly

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

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 is the short name for the referenced assembly.

;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 [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 specifies that the assembly being referred to is referenced from the local assembly.

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

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

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

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

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

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.

Sheridan
  • 68,826
  • 24
  • 143
  • 183