ToB企服应用市场:ToB评测及商务社交产业平台

标题: Avalonia PathIcon使用 [打印本页]

作者: 温锦文欧普厨电及净水器总代理    时间: 2024-4-2 23:43
标题: Avalonia PathIcon使用
PathIcon是一个Avalonia内置的控件,可以根据Geometry绘制一个图标。
源码

PathIcon间接继承TemplatedControl,只有一个Geometry类型的依赖属性Data:
  1. public class PathIcon : IconElement
  2. {
  3.     static PathIcon()
  4.     {
  5.         AffectsRender<PathIcon>(DataProperty);
  6.     }
  7.     public static readonly StyledProperty<Geometry> DataProperty =
  8.         AvaloniaProperty.Register<PathIcon, Geometry>(nameof(Data));
  9.     public Geometry Data
  10.     {
  11.         get { return GetValue(DataProperty); }
  12.         set { SetValue(DataProperty, value); }
  13.     }
  14. }
  15. public abstract class IconElement : TemplatedControl
  16. {
  17. }
复制代码
外观也很简单,一个Path,Data绑定PathIcon的属性Data,填充色绑定属性Foreground:
  1. <ControlTheme x:Key="{x:Type PathIcon}" TargetType="PathIcon">
  2.   <Setter Property="Background" Value="Transparent" />
  3.   <Setter Property="Height" Value="{DynamicResource IconElementThemeHeight}" />
  4.   <Setter Property="Width" Value="{DynamicResource IconElementThemeWidth}" />
  5.   <Setter Property="Template">
  6.     <ControlTemplate>
  7.       <Border Background="{TemplateBinding Background}">
  8.         <Viewbox Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
  9.           <Path Fill="{TemplateBinding Foreground}" Data="{TemplateBinding Data}" Stretch="Uniform" />
  10.         </Viewbox>
  11.       </Border>
  12.     </ControlTemplate>
  13.   </Setter>
  14. </ControlTheme>
复制代码
编辑Geometry

首先要创建Geometry,Avalonia UI Fluent Icons提供了一系列的图标集合,见https://avaloniaui.github.io/icons.html。获取所需的图标后,如果需要微调,可以使用Blend进行调整。
比如创建一个登陆窗口,需要一个账号、密码图标:
使用PathIcon

  1. <ResourceDictionary xmlns="https://github.com/avaloniaui"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.    
  4.         <StreamGeometry x:Key="login">Blend编辑的账号图标...</StreamGeometry>
  5.         <StreamGeometry x:Key="password">Blend编辑的密码图标...</StreamGeometry>
  6. </ResourceDictionary>
复制代码
  1. <Application.Resources>
  2.         <ResourceDictionary>
  3.                 <ResourceDictionary.MergedDictionaries>
  4.                         <ResourceInclude Source="avares://PathIconUsing/Contents/Icons.axaml"/>
  5.                 </ResourceDictionary.MergedDictionaries>
  6.         </ResourceDictionary>
  7. </Application.Resources>
复制代码

  1. <Application.Resources>
  2.         <ResourceDictionary>
  3.                 <ResourceDictionary.MergedDictionaries>
  4.                         <ResourceInclude Source="avares://PathIconUsing/Contents/Icons.axaml"/>
  5.                 </ResourceDictionary.MergedDictionaries>
  6.         </ResourceDictionary>
  7. </Application.Resources><Application.Resources>
  8.         <ResourceDictionary>
  9.                 <ResourceDictionary.MergedDictionaries>
  10.                         <ResourceInclude Source="avares://PathIconUsing/Contents/Icons.axaml"/>
  11.                 </ResourceDictionary.MergedDictionaries>
  12.         </ResourceDictionary>
  13. </Application.Resources>       
复制代码
效果如下图:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4