可重复使用的多边形

发布于 2024-10-15 18:45:37 字数 894 浏览 2 评论 0 原文

我想要在 xaml 中放置一些图标的画布。这些图标是像这样的多边形:

<Polygon Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>

但是我想多次使用一个图标,所以我想在资源中定义它,并通过引用将其包含到画布中的某个位置,就像这样:

<Page.Resources>
   <Polygon Key="icon1" Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon Key="icon2" Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
   <Polygon Reference="icon1" X="0" Y="0"/>
   <Polygon Reference="icon2" X="10" Y="10"/>   
   <Polygon Reference="icon1" X="20" Y="20"/>   
   ...          
</Canvas>

我找到了一个可能的解决方案http://www.codeproject.com/KB/WPF/GraphicInXAMLAndWPF.aspx其中多边形存储在绘图图像中,但似乎开销太大。

有人有更好的想法如何解决这个问题吗?

I want to have a canvas in xaml where i place some icons. These icons are polygons like this one:

<Polygon Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>

But i want to use an icon several times, so i want to define it in the resources and include it by reference into the canvas at a certain position, someway like this:

<Page.Resources>
   <Polygon Key="icon1" Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon Key="icon2" Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
   <Polygon Reference="icon1" X="0" Y="0"/>
   <Polygon Reference="icon2" X="10" Y="10"/>   
   <Polygon Reference="icon1" X="20" Y="20"/>   
   ...          
</Canvas>

I found a possible solution on http://www.codeproject.com/KB/WPF/GraphicInXAMLAndWPF.aspx where the polygons are stored in a drawing image, but seems to be to much overhead.

Someone has an better idea how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-10-22 18:45:37

也许最明显和最灵活的方法是创建一个 UserControl。您可以从解决方案资源管理器添加 UserControl 类型的新文件,将多边形添加到 Visual Studio 将创建的“LayoutRoot”网格。然后,您可以根据需要创建任意数量的用户控件实例!

但是,检查 SO 上的类似问题,您可以使用内容控件来渲染多边形,注意,您必须使用 x:Shared="false" 来确保您不会每次都尝试重复使用相同的多边形。

<Page.Resources>
   <Polygon x:Key="icon1"  x:Shared="False"
            Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon x:Key="icon2"  x:Shared="False"
            Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="0"/>
    <ContentControl Content="{StaticResource icon2}" Canvas.Top="0" Canvas.Left="10"/>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="20"/>
   ...          
</Canvas>

请参阅以下内容:

矢量图像作为可重用的 XAML 片段

Probably the most obvious and flexible method is to create a UserControl. You can add a new file of type UserControl from the solution explorer, add your Polygon to the 'LayoutRoot' Grid that Visual Studio will create. You can then create as many instances as you like of your user control!

However, checking for similar problems on SO, you could use a content control to render the polygon, note, you would have to use x:Shared="false" to ensure that you are not trying to re-use the same polygon each time.

<Page.Resources>
   <Polygon x:Key="icon1"  x:Shared="False"
            Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon x:Key="icon2"  x:Shared="False"
            Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="0"/>
    <ContentControl Content="{StaticResource icon2}" Canvas.Top="0" Canvas.Left="10"/>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="20"/>
   ...          
</Canvas>

See the following:

Vector image as reusable XAML fragment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文