使用XAML / WPF做一些本应该很简单的事情似乎有很多问题--我用矩形和椭圆等形状创建了一些基于XAML的图像来创建图标,这些图标需要我的应用程序的其他部分使用--但我似乎找不到如何做到这一点--我似乎能够在资源字典中存储画布,但无法在任何其他窗口中使用它。如何做到这一点-这些是简单的图像,只是两个或三个形状,我想在我的项目中使用!
图像也必须是可调整大小的-我知道如何存储路径,但是这些形状包含我想要保留的渐变样式,加上我不知道如何将矩形转换为路径和颜色数据。
谢谢!
发布于 2009-02-03 05:51:00
您应该使用绘图,并使用DrawingBrush来显示它,就像KP禤浩焯建议的那样,或者使用DrawingImage和图像控件,但是如果您不能使用绘图,您可以在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>
发布于 2009-02-02 12:19:36
您不希望使用画布将这些资源存储在资源字典中。几何体的根可能类似于DrawingBrush (特别是在使用Expression Design创建图像的情况下),这些项需要添加到资源字典中,如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DrawingBrush x:Key="YourResourceKey">
<DrawingBrush.Drawing>
<DrawingGroup>
<!-- This can change a lot, but a typical XAML file exported from a Design image would have the geometry of the image here as a bunch of Paths or GeometryDrawings -->
</DrawingGroup>
</DrawingBrush.Drawing>
</ResourceDictionary>
我假设您知道如何在您的应用程序中引用此资源字典。
要使用这些资源,您只需将它们分配给适当的属性。对于形状类型的图像,您可以将它们分配给类似矩形的填充属性(还有很多其他方法,但这只是一个简单的方法)。这里有一个例子:
<Button>
<Grid>
<Rectangle Fill="{StaticResource YourResourceKey}" />
</Grid>
</Button>
https://stackoverflow.com/questions/503873
复制