tags:

views:

32

answers:

2

I have some XAML to draw a logo and I want to re-use this in various other XAML files (it has no UI and requires no code). The top level of the logo XAML is a Canvas item.

To use the logo in other XAML files is it best to define this logo as an element in a ResourceDictionary or create a UserControl?

This seems easy with a UserControl, however I want to load my XAML files in with XamlReader so I would prefer to use resources so that these can be specified within the XAML. It seems possible to store items such as a Canvas in a ResourceDictionary but I am not sure how to reference them.

For example, I can define my logo as a ResourceDictionary element as follows:

  <ResourceDictionary
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
            <Canvas x:Name="LayoutRoot" x:Key="Logo">
               <!-- My Logo -->
             </Canvas>
    </ResourceDictionary>

But how can I use this Logo in my other XAML files - maybe I have got the wrong idea about resources?

A: 

Typically, if you want to make a resource dictionary for this, you'd make a style. You could then drop a control, and style it with your resource, in any other xaml file you wished.

However, a UserControl would potentially be an easier option, since it would be a simple, reusable element you could drop in anywhere.

Reed Copsey
I don't think a style is the way to go my logo is far too complicated to convert to a style (i.e >250 lines of XAML)
Mister Cook
That's find for a style. If you're worried, though, you could use a user control.
Reed Copsey
A: 

I found the solution is a VisualBrush. I can define this in the ResourceDictionary as follows:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
<VisualBrush x:Key="Logo">
    <VisualBrush.Visual>
<Canvas x:Name="LayoutRoot" x:Key="Logo"> 
               <!-- My Logo --> 
             </Canvas> 

Then just use this VisualBrush where necessary.

Mister Cook