0

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">
    <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?

4

2 回答 2

1

我发现解决方案是 VisualBrush。我可以在 ResourceDictionary 中定义如下:

<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> 

然后在必要时使用这个 VisualBrush。

于 2010-01-12T18:50:35.440 回答
0

通常,如果您想为此制作资源字典,您会制作样式。然后,您可以在您希望的任何其他 xaml 文件中放置一个控件,并使用您的资源对其进行样式设置。

但是,UserControl 可能是一个更简单的选择,因为它是一个简单、可重用的元素,您可以放在任何地方。

于 2010-01-12T18:03:39.003 回答