引用两次相同的Path资源只会绘制一个 [英] Referencing twice the same Path resource only draws one

查看:59
本文介绍了引用两次相同的Path资源只会绘制一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将路径引用为资源,并在控件中多次显示它,但是有些我不了解.这是我的XAML代码:

Hi everyone,

I''d like to reference a Path as a resource and display it several times in a control but there''s something I don''t understand. Here is my XAML code:

<Page

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid Width="20" Height="20">
  <Grid.Resources>
    <!-- I declare a path as a reusable resource -->
    <Path x:Key="myPath"

          Fill="Blue">
      <Path.Data>
        <GeometryGroup>
          <RectangleGeometry Rect="0,0,10,10" />
          <RectangleGeometry Rect="2,2,6,6" />
        </GeometryGroup>
      </Path.Data>
    </Path>
    <!-- exactly the same resource: just the key changes -->
    <Path x:Key="myPath2"

          Fill="Blue">
      <Path.Data>
        <GeometryGroup>
          <RectangleGeometry Rect="0,0,10,10" />
          <RectangleGeometry Rect="2,2,6,6" />
        </GeometryGroup>
      </Path.Data>
    </Path>
  </Grid.Resources>
  <!-- I would like to reference myPath two times -->
  <ContentControl HorizontalAlignment="Center"

                  VerticalAlignment="Center"

                  Content="{StaticResource myPath}" />
  <!-- if I write myPath instead of myPath2, the first path is not drawn -->
  <ContentControl HorizontalAlignment="Left"

                  VerticalAlignment="Bottom"

                  Content="{StaticResource myPath2}" />
  </Grid>
</Page>



当然,我想摆脱myPath2 ...
我想发生这种情况是因为myPath仅实例化了一次,但是我不知道如何在XAML中创建第二个实例.

有任何想法吗?



Of course, I would like to get rid of the myPath2...
I suppose this happens because myPath is instanciated only once, but I don''t know how to create a second instance in XAML.

Any idea?

推荐答案

好,我自己发现了.我没有将Path对象声明为资源,而是为路径声明了Style.然后,我创建了2条路径并将样式应用于它们.

对于那些可能感兴趣的人,这里的代码是:
OK I found by myself. Instead of declaring a Path object as a resource, I declared a Style for my paths. Then I created 2 paths and applied the style on them.

For those who might be interested, here is the code:
<Page

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Width="20" Height="20">
  <Grid.Resources>
    <!-- the style to be shared -->
    <Style x:Key="myPathStyle" TargetType="Path">
      <Setter Property="Fill" Value="Blue" />
      <Setter Property="Data">
        <Setter.Value>
          <GeometryGroup>
            <RectangleGeometry Rect="0,0,10,10" />
            <RectangleGeometry Rect="2,2,6,6" />
          </GeometryGroup>
        </Setter.Value>
      </Setter>
    </Style>
  </Grid.Resources>
  <!-- the first path -->
  <Path HorizontalAlignment="Center"

        VerticalAlignment="Center"

        Style="{StaticResource myPathStyle}" />
  <!-- the second path -->
  <Path HorizontalAlignment="Left"

        VerticalAlignment="Bottom"

        Style="{StaticResource myPathStyle}" />
  </Grid>
</Page>


这篇关于引用两次相同的Path资源只会绘制一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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