我能够在WPF中创建条带模式,但是如何在XAML中创建类似的模式? WPF中是否有默认的类似画笔?
最佳答案
您可以使用VisualBrush
在XAML中进行操作。您只需要为Path
指定数据值,例如:XAML
<Window.Resources>
<VisualBrush x:Key="MyVisualBrush" TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Grid Background="Black">
<Path Data="M 0 15 L 15 0" Stroke="Gray" />
<Path Data="M 0 0 L 15 15" Stroke="Gray" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Window.Resources>
<Grid Background="{StaticResource MyVisualBrush}">
<Label Content="TEST" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
Output
要将
Image
转换为矢量图形(路径),请使用Inkscape
,它是免费的,非常有用。有关更多信息,请参见以下链接:Vectorize Bitmaps to XAML using Potrace and Inkscape
Edit
为了获得更好的性能,您可以在
Freeze()
的帮助下PresentationOptions
进行刷子,如下所示:<Window x:Class="MyNamespace.MainWindow"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" ...>
<VisualBrush x:Key="MyVisualBrush" PresentationOptions:Freeze="True" ... />
引用
MSDN
:当您不再需要修改可冻结对象时,冻结它可提供性能优势。如果在此示例中冻结画笔,则图形系统将不再需要监视其更改。图形系统还可以进行其他优化,因为它知道画笔不会改变。