Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using x:Shared="False" resources in external assembly in WPF

In our project we have a ResourceDictionary with some Icons that looks like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Canvas x:Key="Icon.Refresh"
            x:Shared="False"
            Width="32"
            Height="32"
            Clip="F1 M 0,0L 32,0L 32,32L 0,32L 0,0">
        <Path .../>
        <Path .../>
        <Path .../>
    </Canvas>
</ResourceDictionary>

The x:Shared="False" attribute is needed, because else the icon would disappear when I use it in multiple views.

Now we want to make another project with the same icons, so we decided to put them in a library project that is referenced by both projects.

But when we try to run the application we always get the error:

Shared attribute in namespace "http://schemas.microsoft.com/winfx/2006/xaml" can be used only in compiled resource dictionaries."

but we can't get rid of the x:Shared="False" attribute, because as far as I know it's the only way to stop the icons from disappearing.

So we what can we do, to share the icons over multiple projects with a project reference and without disappearing icons?

like image 483
Staeff Avatar asked Feb 27 '14 11:02

Staeff


2 Answers

Judging by this error, we can understand that x:Shared attribute can be used only for compiled ResourceDictionary. Quote from MSDN x:Shared Attribute:

The ResourceDictionary that contains the items with x:Shared must be compiled. The ResourceDictionary cannot be within loose XAML or used for themes.

Compiled ResourceDictionary is one that Build action to set Page, as in this case, it is converted to BAML (Binary Application Markup Language) at run-time. This attribute usually be set by default when creating new ResourceDictionary.

BAML is simply XAML that has been parsed, tokenized, and converted into binary form to increase performance for working with XAML files. Quote from Adam Nathan WPF book:

BAML is not like Microsoft intermediate language (MSIL); it is a compressed declarative format that is faster to load and parse (and smaller in size) than plain XAML. BAML is basically an implementation detail of the XAML compilation process.

Therefore it is always advisable to check this flag in ResourceDictionary, because if it will be set Resource, in the memory will be stored not packaged version of XAML, which later may affect to the performance of the whole WPF application.

like image 144
Anatoliy Nikolaev Avatar answered Oct 05 '22 09:10

Anatoliy Nikolaev


Could you try setting the Build action to "Page" instead of "Resources", as mentioned here:

-https://connect.microsoft.com/VisualStudio/feedback/details/776631/using-x-shared-in-a-resourcedictionary-prevents-you-from-setting-the-file-build-action-to-resource

like image 21
CSmith Avatar answered Oct 05 '22 09:10

CSmith