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 t开发者_运维百科op 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">
<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?
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">
<VisualBrush x:Key="Logo">
<VisualBrush.Visual>
<Canvas x:Name="LayoutRoot" x:Key="Logo">
<!-- My Logo -->
</Canvas>
Then just use this VisualBrush where necessary.
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.
加载中,请稍侯......
精彩评论