Theme manager for Avalonia applications.
Theme manager searches user provided themes directory for *.xaml
theme files otherwise built-in Light
and Dark
theme are used.
The ThemeSelector
is created and initalized by calling static Create
method.
The ThemeSelector
uses Styles[0]
property of Window
to insert selected theme Style
.
App.xaml
<Application x:Class="AvaloniaApp.App"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
</Application.Styles>
</Application>
App.xaml.cs
using System;
using Avalonia;
using Avalonia.Logging.Serilog;
using Avalonia.Markup.Xaml;
using Avalonia.ThemeManager;
namespace AvaloniaApp
{
public class App : Application
{
public static ThemeSelector Selector;
[STAThread]
static void Main(string[] args)
{
BuildAvaloniaApp().Start(AppMain, args);
}
static void AppMain(Application app, string[] args)
{
Selector = ThemeSelector.Create("Themes");
Selector.LoadSelectedTheme("AvaloniaApp.theme");
app.Run(new MainWindow());
Selector.SaveSelectedTheme("AvaloniaApp.theme");
}
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToDebug();
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
}
}
MainWindow.xaml
<Window x:Class="AvaloniaApp.MainWindow"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:AvaloniaApp;assembly=AvaloniaApp"
xmlns:manager="clr-namespace:Avalonia.ThemeManager;assembly=Avalonia.ThemeManager"
Title="AvaloniaApp" Width="800" Height="600"
Foreground="{DynamicResource ThemeForegroundBrush}">
<Window.Resources>
<manager:ObjectEqualityMultiConverter x:Key="ObjectEqualityMultiConverter"/>
</Window.Resources>
<Grid RowDefinitions="Auto,*">
<Menu Grid.Row="0">
<MenuItem Header="_View">
<MenuItem Header="_Theme" DataContext="{x:Static app:App.Selector}" Items="{Binding Themes}">
<MenuItem.Styles>
<Style Selector="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding Selector.ApplyTheme}"/>
<Setter Property="CommandParameter" Value="{Binding}"/>
<Setter Property="Icon">
<Template>
<CheckBox>
<CheckBox.IsChecked>
<MultiBinding Mode="OneWay" Converter="{StaticResource ObjectEqualityMultiConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource Self}"/>
<Binding Path="Selector.SelectedTheme"/>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</Template>
</Setter>
</Style>
</MenuItem.Styles>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
MainWindow.xaml.xs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ThemeManager;
namespace AvaloniaApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.AttachDevTools();
App.Selector.EnableThemes(this);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
The EnableThemes(...);
can be used to enable themes for multiple windows.
Avalonia theme manager is delivered as a NuGet package.
You can find the packages here NuGet and install the package like this:
Install-Package Avalonia.ThemeManager
or by using nightly build feed:
- Add
https://www.myget.org/F/avaloniathememanager-nightly/api/v2
to your package sources - Alternative nightly build feed
https://pkgs.dev.azure.com/wieslawsoltes/GitHub/_packaging/Nightly/nuget/v3/index.json
- Update your package using
Avalonia.ThemeManager
feed
and install the package like this:
Install-Package Avalonia.ThemeManager -Pre
Avalonia.ThemeManager is licensed under the MIT license.