c# - 如何在用户控件中添加 Canvas xaml资源

标签 c# wpf xaml

我已经下载了这个包:http://modernuiicons.com/我正在尝试使用 xaml 图标。

我已将包含以下内容的 xaml 文件添加到我的解决方案

<?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_check" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="37.9998" Height="31.6665" Canvas.Left="19.0001" Canvas.Top="22.1668" Stretch="Fill" Fill="#FF000000" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z "/>
</Canvas>

现在,如何将此 Canvas 引用到我的用户控件?

用户控制

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
mc:Ignorable="d"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" Background="#FF2F2F2F" BorderBrush="#FF919191">


<Grid x:Name="LayoutRoot" Height="50" RenderTransformOrigin="0.5,0.5">
    <Rectangle x:Name="rectangle" RenderTransformOrigin="0.5,0.5" Width="230" Height="50"/>
    <TextBlock x:Name="NameLabel" FontSize="16" Foreground="#FFE5E5E5" Height="34" Width="149" Text="Onthaal Telefoon" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0" FontFamily="Segoe UI Semibold"/>
    <Viewbox HorizontalAlignment="Right" VerticalAlignment="Top" Height="16.5" Width="17.789" Margin="0,15,24.5,0">
        // Here I want to reference the canvas
    </Viewbox>
</Grid>
</UserControl>

我可以复制 Canvas 的内容,但必须有其他解决方案。

最佳答案

CanvasPath 作为资源添加到页面或 App.xaml 或其他任何地方,记得设置 x:Key .然后使用 ContentControl 来引用资源。

<!-- In Resources, either on the Page or App.xaml for app-wide reuse -->
<Canvas x:Key="TickCanvas" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_check" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="37.9998" Height="31.6665" Canvas.Left="19.0001" Canvas.Top="22.1668" Stretch="Fill" Fill="#FF000000" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z "/>
</Canvas

<!-- On your page, or somewhere -->
<ViewBox>
    <ContentControl Content="{StaticResource TickCanvas}" />
</ViewBox>

作为它有效的证明,我能够看到它的一个勾号!

请注意,我通常只获取路径数据、迷你标记并将其保存为字符串资源。然后使用 Path 我通过 Data={StaticResource TickPath} 引用标记资源,这样我就可以使用 Height 调整矢量的大小Path 本身的 >Width 或通过设置 Stretch="Uniform" 让它由其父级扩展和收缩。节省了 Viewbox 的开销。

<!-- In App.xaml for app-wide reuse -->
<x:String x:Key="TickPath">F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z </x:String>

<!-- On page, template or wherever -->
<Path Data="{StaticResource TickPath} />

这种技术在这种情况下可能不起作用,因为那里有一个剪辑几何体。但是对于简单的矢量来说很好,我有手绘字体(不能作为字体嵌入)作为标记存储在文件中,然后我在运行时加载它们 - Data={Binding PathData} 有效也一样。

关于c# - 如何在用户控件中添加 Canvas xaml资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18657162/

相关文章:

c# - 如何创建一个以名称为参数的表?

c# - 当使用可执行文件的完整路径,以管理员身份运行时,Windows 10 无法访问指定的设备、路径或文件

c# - wpf - 将数据上下文绑定(bind)到单例类的静态属性

c# - 如何以增量方式调整 WPF 窗口的大小?

xaml - UWP ListView DataTemplate 绑定(bind)到项目而不是属性

c# - X509Store.Certificates.Find 将 validOnly 设置为 true 不会返回中间机构证书

c# - 快速简便的 setter 和 setter/getter ?

c# - 在不可预知的时间禁用和再次启用控件时维护 Tab 键顺序

c# - 如何在 WPF 自定义 Adorner 中用线条排列 Thumbs

xaml - ui没有实时更新