A library to easily display icons in an Avalonia App.
Name | Description | Version |
---|---|---|
Projektanker.Icons.Avalonia | Core library | |
Projektanker.Icons.Avalonia.FontAwesome | Font Awesome Free | |
Projektanker.Icons.Avalonia.MaterialDesign | Material Design Icons |
Name | Prefix | Example |
---|---|---|
FontAwesome | fa |
fa-github |
MaterialDesign | mdi |
mdi-github |
A full example is available in the demo directory.
Register the icon provider(s) within the Main
method before building the Avalonia app. Otherwise the visual designer won't be able to use the registered icon provider(s).
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args)
{
// Register icon provider(s)
IconProvider.Register<FontAwesomeIconProvider>();
IconProvider.Register<MaterialDesignIconProvider>();
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace();
}
}
Add xmlns:i="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
to your view.
Example: The GitHub icon.
FontAwesome
<i class="fab fa-github"></i>
Standalone
<i:Icon Value="fab fa-github" />
Attached
<Button i:Attached.Icon="fab fa-github" />
Just implement the IIconProvider
interface:
namespace Projektanker.Icons.Avalonia
{
/// <summary>
/// Represents an icon provider.
/// </summary>
public interface IIconProvider
{
/// <summary>
/// Gets the prefix of the <see cref="IIconProvider"/>.
/// </summary>
string Prefix { get; }
/// <summary>
/// Gets the SVG path of the requested icon using the registered icon providers.
/// </summary>
/// <param name="value">The value specifying the icon to return it's path from.</param>
/// <returns>The path of the icon.</returns>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">The icon associated
/// with the specified <paramref name="value"/> does not exists.</exception>
string GetIconPath(string value);
}
}
and register it with the IconProvider
:
IconProvider.Register<MyCustomIconProvider>()
or
IIconProvider provider = new MyCustomIconProvider(/* custom ctor arguments */);
IconProvider.Register(provider);
The IIconProvider.Prefix
property has to be unique within all registered providers. It is used to select the right provider. E.g. FontAwesomeIconProvider
's prefix is fa
.