Avalonia PathIcon使用
  9txZwuyI7VfP 2024年01月17日 24 0

PathIcon是一个Avalonia内置的控件,可以根据Geometry绘制一个图标。

源码

PathIcon间接继承TemplatedControl,只有一个Geometry类型的依赖属性Data:

public class PathIcon : IconElement
{
    static PathIcon()
    {
        AffectsRender<PathIcon>(DataProperty);
    }

    public static readonly StyledProperty<Geometry> DataProperty =
        AvaloniaProperty.Register<PathIcon, Geometry>(nameof(Data));

    public Geometry Data
    {
        get { return GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }
}

public abstract class IconElement : TemplatedControl
{
}

外观也很简单,一个Path,Data绑定PathIcon的属性Data,填充色绑定属性Foreground:

<ControlTheme x:Key="{x:Type PathIcon}" TargetType="PathIcon">
  <Setter Property="Background" Value="Transparent" />
  <Setter Property="Height" Value="{DynamicResource IconElementThemeHeight}" />
  <Setter Property="Width" Value="{DynamicResource IconElementThemeWidth}" />
  <Setter Property="Template">
    <ControlTemplate>
      <Border Background="{TemplateBinding Background}">
        <Viewbox Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
          <Path Fill="{TemplateBinding Foreground}" Data="{TemplateBinding Data}" Stretch="Uniform" />
        </Viewbox>
      </Border>
    </ControlTemplate>
  </Setter>
</ControlTheme>

编辑Geometry

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

比如创建一个登陆窗口,需要一个账号、密码图标:

  1. 打开Blend,创建一个WPF项目,新建一个UserControl;
  2. 从Fluent Icons中查找账号相关的图标,复制Geometry的内容;
  3. 在UserControl的布局中,添加一个Path,Data属性粘贴上述复制的内容;
  4. 在编辑窗口中新增、删除或拖动控制点进行微调,在属性窗口中可以选择填充色、画笔颜色等;
  5. 重复2-3-4,编辑密码图标;

使用PathIcon

  1. 在创建的Avalonia项目中,添加一个资源字典文件,将使用Blend编辑的Geometry复制过来:
<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- Add Resources Here -->
	<StreamGeometry x:Key="login">Blend编辑的账号图标...</StreamGeometry>
	<StreamGeometry x:Key="password">Blend编辑的密码图标...</StreamGeometry>
</ResourceDictionary>
  1. 将资源文件添加到App资源中:
<!-- App.axaml -->
<Application.Resources>
	<ResourceDictionary>
		<ResourceDictionary.MergedDictionaries>
			<ResourceInclude Source="avares://PathIconUsing/Contents/Icons.axaml"/>
		</ResourceDictionary.MergedDictionaries>
	</ResourceDictionary>
</Application.Resources>
  1. 编辑登陆窗口视图:
  • 添加两个TextBox,一个按钮,并调整布局;
  • TextBox的InnerLeftContent内容使用PathIcon控件,PathIcon的Data属性使用1中定义的Geometry,Foreground属性使用在Blend中设置的填充色;
<Grid RowDefinitions="*,*,*" Height="150" Width="250">
	<TextBox VerticalAlignment="Center">
		<TextBox.InnerLeftContent>
			<PathIcon Data="{DynamicResource ResourceKey=login}" Margin="5" Foreground="#FF1AA2D9"/>
		</TextBox.InnerLeftContent>
	</TextBox>
	<TextBox Grid.Row="1" VerticalAlignment="Center">
		<TextBox.InnerLeftContent>
			<PathIcon Data="{DynamicResource ResourceKey=password}" Margin="5" Foreground="#FF1AA2D9"/>
		</TextBox.InnerLeftContent>
	</TextBox>
	<Button Content="登陆" Grid.Row="3" Width="250"
			HorizontalContentAlignment="Center" Background="#FF1AA2D9" Foreground="White"/>
</Grid>

效果如下图:

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年01月17日 0

暂无评论

推荐阅读
  NPQODODLqddb   2024年05月17日   70   0   0 .NET
  mVIyUuLhKsxa   2024年05月17日   53   0   0 .NET
  XkHDHG7Y62UM   2024年05月17日   45   0   0 .NET
  f18CFixvrKz8   2024年05月18日   88   0   0 .NET
  rBgzkhl6abbw   2024年05月18日   78   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   41   0   0 .NET
  S34pIcuyyIVd   2024年05月17日   60   0   0 .NET
  gKJ2xtp6I8Y7   2024年05月17日   55   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   40   0   0 .NET
9txZwuyI7VfP