矢量图像可重复使用的XAML碎片 [英] Vector image as reusable XAML fragment

查看:83
本文介绍了矢量图像可重复使用的XAML碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重用一些XAML片段作为在某些WPF应用程序/库图片

I want to reuse some XAML fragment as image in some WPF application/library.

问题的背景是以下内容:

The background of the problem is following:

这很容易在WPF应用程序重复使用位图图像。图像可以被添加作为一种资源,我可以使用<图像源=packURI/> 在XAML许多地方因此图像将是相同的

It's easy to reuse a bitmap image in a WPF application. The image can be added as a resource, I can use <Image Source="packURI"/> at many places in XAML so the image will be the same.

不过,我想有可能做同样的矢量图像。图像本身可以表示为路径,但我不能重复使用相同的路径作为一种资源,因为简单地使用它在几个不同的地方(也可能是从几个UI线程)被禁止(UI元素只能有一个逻辑父)。

But I'd like to have a possibility to do the same for a vector image. The image itself can be represented as Path, but I cannot reuse the same Path as a resource, because simply using it at several different places (and possibly from several UI threads) is prohibited (UI element can have only one logical parent).

此外,该问题得到,如果我是更复杂倒要几个打造的形象路径 S,使用画布它。或一些任意XAML代码。

Moreover, the question gets more complicated if I'd like to build the "image" from several Paths, use a Canvas for it. Or some arbitrary XAML code.

我试图用一个风格路径,所以图像被以这样的方式来表示:

I tried using a Style for the Path, so the image is represented in such a way:

<Path Style={StaticResource VectorImage1}/>

这似乎是一个可重用的方式,但我关心的是两个问题:

This seems to be a reusable way, but I am concerned with two problems:


  1. 如果一个矢量图像的实现是由路径更改为(例如)画布,我需要来取代它不仅在款式,却处处在使用它的源代码。

  2. 路径的定义使用样式似乎过于冗长。

  3. 我看到没有办法为期广义这种方法使用画布或任意XAML代码。

  4. 的语法似乎是相当不自然。

  1. If the implementation of a vector image is changed from Path to a (for instance) Canvas, I'll need to replace it not only in the style, but everywhere in the source code which uses it.
  2. Definition of a path using a style seems to be too verbose.
  3. I see no way to generalize this approach for using Canvas or arbitrary XAML code.
  4. The syntax seems to be quite unnatural.

有其他的办法有一个可重复使用XAML片段,通过定义一个用户控件,但定义每个矢量图像一个单独的用户控件似乎是矫枉过正。

There is other way to have a reusable XAML fragment, through defining a UserControl, but defining a separate user control for each vector image seems to be an overkill.

有没有更好的,漂亮的,正确的定义可重复使用XAML片段的方式?

Is there a better, nice, right way to define a reusable XAML fragment?

推荐答案

您共享属性的路径和资源使用它作为一个静态资源:可以在x补充。这将工作,如果MyVectorImage变为别的东西。

You can add the x:Shared attribute to the Path Resource and use it as a StaticResource. This will work if "MyVectorImage" changes to something else

更新结果
也许最好使用ContentControl中或类似。可以添加属性,如保证金等

Update
Probably better to use a ContentControl or similar to be able to add Properties, such as Margin etc.

<Window.Resources>
    <Path x:Key="MyVectorImage"
          x:Shared="False"
          Stroke="DarkGoldenRod"
          StrokeThickness="3"
          Data="M 10,20 C 10,25 40,35 40,17 H 28"
          Stretch="Fill"
          Width="100"
          Height="40"/>
</Window.Resources>
<StackPanel>
    <ContentControl Margin="10" Content="{StaticResource MyVectorImage}"/>
    <ContentControl Margin="10" Content="{StaticResource MyVectorImage}"/>
</StackPanel>



为例。你与含有两个路径的StackPanel替换MyVectorImage

Example. You replace "MyVectorImage" with a StackPanel containing two Paths.

<Window.Resources>
    <StackPanel x:Key="MyVectorImage"
                x:Shared="False">
        <Path Stroke="DarkGoldenRod"
              StrokeThickness="3"
              Data="M 10,20 C 10,25 40,35 40,17 H 28"
              Stretch="Fill"
              Width="100"
              Height="40"/>
        <Path Stroke="DarkGoldenRod"
              StrokeThickness="3"
              Data="M 10,20 C 10,25 40,35 40,17 H 28"
              Stretch="Fill"
              Width="100"
              Height="40"/>
    </StackPanel>
</Window.Resources>

这篇关于矢量图像可重复使用的XAML碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆