tags:

views:

64

answers:

1

I'm adding a new WPF project to an existing Visual Studio solution and would like to reuse a bunch of code (C# and xaml) from an existing project within the solution.

I've created the new project and added existing files as follows:

  • Right click project
  • Add -> Add Existing Item
  • Find the file to reuse, use the arrow next to "Add" and "Add as Link"

I now have a nice project set up with all the proper links. However, XAML chokes on these links. For example:

<ResourceDictionary.MergedDictionaries>
       <ResourceDictionary
           Source="Resources\Elements\Buttons\Buttons.xaml" />
       <ResourceDictionary
           Source="Resources\Elements\TextBox\TextBox.xaml" />
 </ResourceDictionary.MergedDictionaries>

The files "Buttons.xaml" and "TextBox.xaml" exist as links in my new project. The project builds, but when I run, I get the following XamlParseException:

'Resources\Elements\Buttons\Buttons.xaml' value cannot be assigned to property 'Source' of object 'System.Windows.ResourceDictionary'. Cannot locate resource 'resources/elements/buttons/buttons.xaml'.

It seems like the XAML parser is requiring an actual copy of these XAML files to exist in my new project, instead of links.

This is exactly what I'm trying to avoid. I want my project to share these files so that any changes get transferred to the other project without hunting and copying.

Any insight is appreciated!

+1  A: 

Linking to an external XAML file does not create a file where the link exists in a project structure, as you have noticed.

My advice is to use relative links in the MergedDictionaries references. If the XAML to reuse in another project is called Common, the Source property of the first nested ResourceDictionary could be:

..\Common\Resources\Elements\TextBox\TextBox.xaml

which is actually the path that you used to add the existing item.

Timores