Theme manager for Avalonia applications.
Перейти к файлу
Wiesław Šoltés 1ba396f41c Update 2023-06-04 22:06:51 +02:00
.github Update build.yml 2021-10-03 22:35:55 +02:00
.nuke Update to .NET 6 2021-11-08 22:58:17 +01:00
build Update 2023-06-04 22:06:51 +02:00
samples/AvaloniaApp Update to preview8 2023-05-10 09:24:50 +02:00
src/Avalonia.ThemeManager Update SimpleThemeManager.cs 2023-02-04 13:22:42 +01:00
tests/Avalonia.ThemeManager.UnitTests Update Avalonia.ThemeManager.UnitTests.csproj 2022-11-04 10:00:12 +01:00
.editorconfig Update .editorconfig 2021-07-18 14:14:30 +02:00
.gitattributes Initial import 2019-04-04 13:27:58 +02:00
.gitignore Update .gitignore 2022-11-17 22:49:00 +01:00
Avalonia.ThemeManager.sln Update to preview8 2023-05-10 09:24:50 +02:00
LICENSE.TXT Initial import 2019-04-04 13:27:58 +02:00
NuGet.Config Update 2023-01-04 20:45:38 +01:00
README.md Update README.md 2021-10-17 22:12:59 +02:00
azure-pipelines.yml Update 2022-11-04 09:57:48 +01:00
build.cmd Update to .NET 6 2021-11-08 22:58:17 +01:00
build.ps1 Update to .NET 6 2021-11-08 22:58:17 +01:00
build.sh Update to .NET 6 2021-11-08 22:58:17 +01:00
global.json Update global.json 2022-11-08 20:56:53 +01:00

README.md

Avalonia Theme Manager

Build Status CI

NuGet NuGet MyGet

Github All Releases GitHub release Github Releases

About

Theme manager for Avalonia applications.

Usage

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.

NuGet

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

License

Avalonia.ThemeManager is licensed under the MIT license.