General project with a resource dictionary (xaml)

I am looking for a way to share a ResourceDictionary between projects.

Adding a new item to a shared project does not offer a resource dictionary. It can be created in another (main) project and dragged. But then I can’t change its build options on the page:

> </a> <a href =enter image description here

The idea is to download a resource dictionary like this

 var dictionary = new ResourceDictionary(); dictionary.Source = new Uri("/WpfApplication91;component/Dictionary2.xaml", UriKind.Relative); 

Currently this is clearly not working with

An exception of type "System.IO.IOException" occurred in PresentationFramework.dll but was not handled in user code

Additional Information: The resource 'dictionary2.xaml' could not be found.

Any ideas?

+3
c # wpf xaml resourcedictionary shared-project
source share
3 answers

You can manually edit the overall project to set the build action for the resource dictionary.

The general project consists of Project.shproj and Project.projitems , open the second one and find the dictionary there:

 <ItemGroup> <None Include="$(MSBuildThisFileDirectory)Dictionary.xaml" /> </ItemGroup> 

Add after that

 <ItemGroup> <Page Include="$(MSBuildThisFileDirectory)Dictionary.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </Page> </ItemGroup> 

this is copy / paste from regular csproj for a WPF project containing a dictionary.

It seems to work, although this build action is not displayed when the project loads in Visual Studio. Adding files to a shared project does not affect this change manually.

Now I can have a generic project containing a resource dictionary, yay!

The resource dictionary can be combined into application dictionaries, for example, if it is located in the root of the project (use as a static / dynamic resource in the xaml designer):

 <Application.Resources> <ResourceDictionary > <ResourceDictionary.MergedDictionaries> <!-- doesn't really exists in project --> <ResourceDictionary Source="Dictionary.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

and / or loaded manually, for example. using pack Uri :

 var dictionary = new ResourceDictionary() { Source = new Uri("pack://application:,,,/FlexProperty.xaml"), }; 
+2
source share

I had the same problem. There is a solution for including Xaml in collaborative projects that do not require direct editing of the .projitems file.

You just need to add Xamarin to your Visual Studio installation. (I did this with VS Community 2015).

Now you can add xaml types through the regular Visual Studio dialog: Add Resource Dictionary

And the correct assembly action is available: Assembly action

Xaml in general projects now compiles and works as expected.

(Presumably this is Xamarin Forms support, but it works for any xaml document.)

+2
source share

This problem is that you put the name of the application. You need a project name. The following shows how to do this in both XAML and code.

 <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/SharedProject1;Component/Dictionary2.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> 

Or

 var dictionary = new ResourceDictionary(); dictionary.Source = new Uri("/SharedProject1;component/Dictionary2.xaml", UriKind.Relative); 
0
source share

All Articles