返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Desktop

Improving Avalonia UI Quality, Cross-Platform Icon Library Selection Practice

This article details methods for using two icon libraries (Icon.Avalonia and IconPacks.Avalonia) within the Avalonia framework. By highlighting inconsistencies in emoji display across different devices, the author introduces the advantages of using FontAwesome icons. The article focuses on the installation steps, XAML configuration methods, and practical application scenarios of the two libraries, including standalone icon display, control attachment, and animation effect implementation. Among them, IconPacks.Avalonia supports 35 icon sets, offering richer functionality and providing an icon browser tool. Throughout, the article offers practical technical references for developers through specific code examples.

曦远 (DealiAxy)约 19 分钟
目录18 个章节

Preface

Previously, I updated the StarBlog publishing tool with a version upgrade and optimized the interface, which included replacing emoji with FontAwesome icons.

If emoji are only used on a single system (like Windows), there might not be any issues.

However, when switching between multiple devices, I found that the rendering on macOS wasn't ideal—some emoji displayed differently than on Windows.

Now, with all icons switched to Font Awesome, the appearance is much more consistent and aesthetically pleasing. It feels like the process of changing icons could be a topic for a separate article.

The Avalonia ecosystem offers a variety of icon libraries to choose from, and this article primarily introduces two of them:

  • Icon.Avalonia
  • IconPacks.Avalonia

Icon.Avalonia

https://github.com/Projektanker/Icons.Avalonia

This library supports two types of icons and currently has 365 stars.

I use this library in the StarBlog Publisher tool, and it's quite simple to use.

Installation

Using FontAwesome as an example, first add the dependency

dotnet add package Projektanker.Icons.Avalonia.FontAwesome

Register in the Program.BuildAvaloniaApp method

public static AppBuilder BuildAvaloniaApp() {
  IconProvider.Current
    .Register<FontAwesomeIconProvider>();

  return AppBuilder.Configure<App>()
    .UsePlatformDetect()
    .LogToTrace();
}

Add the namespace in the layout file

xmlns:i="https://github.com/projektanker/icons.avalonia"

Usage

Then you can use the icons.

Standalone usage

<i:Icon Value="fa-brands fa-anchor" />

Display a single icon.

Attached to ContentControl (e.g., a Button)

<Button i:Attached.Icon="fa-brands fa-anchor" />

Attach the icon to controls like buttons.

Attached to MenuItem

<MenuItem Header="About" i:MenuItem.Icon="fa-solid fa-circle-info" />

Add an icon to menu items, e.g., the "About" menu.

Custom icon size

<i:Icon Value="fa-brands fa-anchor" FontSize="24" />

Set the icon font size to 24.

Animated

<i:Icon Value="fa-spinner" Animation="Pulse" />
<i:Icon Value="fa-sync" Animation="Spin" />

Add animation to icons:

  • Pulse: a pulsing effect

  • Spin: a spinning effect

As an Image source

<Image>
  <Image.Source>
    <i:IconImage Value="fa-brands fa-anchor" Brush="(default is black)" />
  </Image.Source>
</Image>

Display the icon as an image. The color can be set via Brush (default is black).

IconPacks.Avalonia

https://github.com/MahApps/IconPacks.Avalonia

This library supports a vast number of icons, though it's a bit surprising that it only has 62 stars.

It supports all of the following icons... 😂, which is incredibly comprehensive!

  • BootstrapIcons
  • BoxIcons
  • CircumIcons
  • Codicons
  • Coolicons
  • Entypo
  • EvaIcons
  • FeatherIcons
  • FileIcons
  • Fontaudio
  • FontAwesome
  • Fontisto
  • ForkAwesome
  • GameIcons
  • Ionicons
  • JamIcons
  • Lucide
  • Material
  • MaterialLight
  • MaterialDesign
  • MemoryIcons
  • Microns
  • Modern
  • Octicons
  • PhosphorIcons
  • PicolIcons
  • PixelartIcons
  • RadixIcons
  • RemixIcon
  • RPGAwesome
  • SimpleIcons
  • Typicons
  • Unicons
  • VaadinIcons
  • WeatherIcons
  • Zondicons

Installation

Here we'll use FontAwesome as an example

dotnet add package IconPacks.Avalonia.FontAwesome

Add the namespace

xmlns:iconPacks="https://github.com/MahApps/IconPacks.Avalonia"

Register it in the Application styles

<Application.Styles>
  <FluentTheme />

  <StyleInclude Source="avares://IconPacks.Avalonia/Icons.axaml" />
  <StyleInclude Source="avares://IconPacks.Avalonia.FontAwesome/Icons.axaml" />
</Application.Styles>

PackIcon Control

The PackIcon control is the primary method for using icons from icon packs.

Each icon pack has its own control, following this naming format:

PackIcon<PackName>

For example, using icons from FontAwesome:

<iconPacks:PackIconFontAwesome Kind="SolidAnchor"
                               Width="24"
                               Height="24" />

Or:

<iconPacks:PackIconFontAwesome Kind="BrandsGithub"
                               Width="24"
                               Height="24" />

Generic Control PackIconControl

PackIconControl is a generic control that can be used to display icons from any icon pack.

Only available in the main package IconPacks.Avalonia.

<iconPacks:PackIconControl Kind="{x:Static iconPacks:PackIconFontAwesomeKind.SolidAnchor}"
                           Width="24"
                           Height="24" />

PackIcon Markup Extension

A more concise approach is to use the PackIcon markup extension to set the content of a ContentControl.

For example, you can add a FontAwesome icon inside a Button:

<Button Content="{iconPacks:FontAwesome SolidAnchor}" />

Or use the parameterized form:

<Button Content="{iconPacks:FontAwesome Kind=SolidAnchor}" />

You can also set the size:

<Button Content="{iconPacks:FontAwesome SolidAnchor, Width=32, Height=32}" />

If you are using the main package, you can also use the generic syntax:

<Button Content="{iconPacks:PackIcon {x:Static iconPacks:PackIconFontAwesomeKind.SolidAnchor}, Width=32, Height=32}" />

PackIcon Image MarkupExtensions

Icons can also be used as images:

<Image Source="{iconPacks:FontAwesomeImage Kind=SolidAnchor, Brush=DarkBlue}"
       Width="24" Height="24"
       HorizontalAlignment="Left" Margin="10" />

Alternatively, a generic syntax can be used (available in the main package):

<Image Source="{iconPacks:PackIconImage Kind={x:Static iconPacks:PackIconFontAwesomeKind.SolidAnchor}, Brush=DarkOrange}"
       Width="24" Height="24"
       HorizontalAlignment="Left" Margin="10" />

PackIcon Image Converter

You can also use an icon converter to transform bound data into an icon image:

<Image Source="{Binding #FontAwesomeKindComboBox.SelectedItem, Mode=OneWay, Converter={iconPacks:PackIconFontAwesomeKindToImageConverter Brush=Crimson}}"
       Width="24" Height="24"
       HorizontalAlignment="Left" Margin="10" />

A universal converter is also available in the main package:

<Image Source="{Binding #FontAwesomeKindComboBox.SelectedItem, Mode=OneWay, Converter={iconPacks:PackIconKindToImageConverter Brush=Crimson}}"
       Width="24" Height="24"
       HorizontalAlignment="Left" Margin="10" />

For more details on the icon types supported by FontAwesome (Solid, Regular, Brands, etc.), you can check the PackIconFontAwesomeKind enumeration or view all available icon names in the IDE's code completion.

Icon Browser Tool

https://github.com/MahApps/IconPacks.Browser

This IconPacks also includes an icon browser, which makes it very convenient to search for icons and copy code.

It is indeed very comprehensive~

However, what I didn't expect is that the icon browser has more Stars (170+) than the icon pack itself (60+)... 😂

_EOF_

Improving Avalonia UI Quality, Cross-Platform Icon Library Selection Practice

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

聊聊几个相见恨晚的WinForm界面库

前阵子在逛知乎的时候看到了Gitee发的文章,里面介绍了一些WinForm开源项目,其中就有涉及到界面库,我一下子就来了兴趣,然后顺瓜摸藤又找到了几个类似的界面库,看着熟悉的界面和代码,有一种「爷的青春回来了」的感觉~ 可惜属于WinForm的短暂时代早已结束,当今Web统治一切,怕是WinForm以后只能用来追忆往事了吧…… SunnyUI 看看作者的截图: 对话框: 看这界面颇有年代感,很惊讶

2025-03-27 · Desktop · 约 1 分钟

AOT编译Avalonia应用:StarBlog Publisher项目实践与挑战

本文介绍了作者使用Avalonia框架开发的跨平台文章发布工具StarBlog Publisher,并详细记录了使用AOT(Ahead-Of-Time)技术发布Avalonia应用的过程及遇到的问题。文章讨论了AOT的优缺点、配置方法,并针对JSON序列化、类型注册等问题提供了解决方案。作者分享了使用Newtonsoft.Json库和rd.xml文件等技巧,以优化AOT发布的性能和兼容性。

2025-04-27 · Desktop · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…