wingerse
wingerse

Reputation: 3796

Pack URI not working for generic.xaml merged dictionaries

I made a custom control library and made a control named "FlipView" in the root path. Then I removed the style in Generic.xaml and moved it to its own Resource dictionary named FlipView.xaml in the root path. Now I merge that resource dictionary into Generic.xaml using the following code:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>

Then I used the control in another wpf project, but it throws a XamlParseException with InnerException saying

Cannot locate resource 'flipview.xaml'.

Why can't it? The Resource Dictionary is in the root path of the control library project.

If I replace the Source property setter with "pack://application:,,,/MyCustomControls;component/FlipView.xaml" (MyCustomControls is the name of my custom control library) it works perfectly fine.

Generic.xaml:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MyCustomControls;component/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>

Why is this the case?

For the WPF projects, this seems redundant because pack://application,,,/ refers to the root path and WpfAssemblyName;component/ again refers to the root path. Why is it necessary for Generic.xaml?

Edit: I have seen this question but it does not explain why.

Upvotes: 1

Views: 1654

Answers (1)

StepUp
StepUp

Reputation: 38189

Cause you use a resource file from another assembly and it is necessary to point an assembly name.

As MSDN says:

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

  • Authority: application:///.

  • Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format:

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

    • AssemblyShortName: the short name for the referenced assembly.
    • ;Version [optional]: 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]: 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: 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

Update:

To avoid redundant words in address to declare styles in the same assembly, it is possible to declare your style file without pointing out to a library:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>

Answer by Clemens:

Take a look at the third figure here. Pack URIs in an application are relative to the application assembly, even if they are used in a library. Your resource is in a referenced assembly, so you'll have to specify its name.

Upvotes: 1

Related Questions