如何在 XAML/WPF 中存储和检索多个形状? [英] How to store and retrieve multiple shapes in XAML/WPF?

查看:38
本文介绍了如何在 XAML/WPF 中存储和检索多个形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在使用 XAML/WPF 做一些应该很简单的事情时遇到很多问题 - 我已经使用 Rectangle 和 Ellipse 等形状创建了一些基于 XAML 的图像来创建我需要应用程序的其他部分才能使用的图标 -但我似乎无法找到如何做到这一点 - 我似乎能够在资源字典中存储画布,但无法在任何其他窗口中使用它.这是如何完成的 - 这些是我想在整个项目中使用的只有两个或三个形状的简单图像!
图像也必须可调整大小 - 我知道如何存储路径,但是这些形状包含我想要保留的渐变样式,而且我不知道矩形如何转换为路径和颜色数据.

Seem to be having a lot of problems doing what should be simple things with XAML / WPF - I have created some XAML-based images using shapes like Rectangle and Ellipse to create icons which I need other parts of my application to use - but I cannot seem to find out how to do this - I seem to be able to store a Canvas in the Resource Dictionary but no way of using it in any other Window. How is this done - these are simple images just two or three shapes I want to use throughout my project!
The images must also be resizable - I know how to store paths, however these shapes contain gradient styles I want preserved plus I don't know how the rectangle could convert to path and colour data.

谢谢!

推荐答案

你应该使用一个绘图并使用像 KP Adrian 建议的那样的 DrawingBrush 或一个 DrawingImage 和一个 Image 控件来显示它,但是如果你不能使用一个绘图你可以在 VisualBrush 中使用 Canvas.

You should use a Drawing and display it using a DrawingBrush like KP Adrian suggested or a DrawingImage and an Image control, but if you can't use a drawing you can use a Canvas inside a VisualBrush.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
    <VisualBrush x:Key="Icon">
        <VisualBrush.Visual>
            <Canvas Width="10" Height="10">
                <Ellipse Width="5" Height="5" Fill="Red"/>
                <Ellipse Width="5" Height="5" Fill="Blue" Canvas.Left="5" Canvas.Top="5"/>
            </Canvas>
        </VisualBrush.Visual>
    </VisualBrush>
</Page.Resources>
    <Rectangle Width="100" Height="100" Fill="{StaticResource Icon}"/>
</Page>

这篇关于如何在 XAML/WPF 中存储和检索多个形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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