Theme manager for Avalonia applications.
Перейти к файлу
Wiesław Šoltés ce1349d579 Update Avalonia.ThemeManager.sln 2019-04-05 17:15:45 +02:00
build Updated version 2019-04-05 11:37:28 +02:00
samples/AvaloniaApp Update MainWindow.xaml.cs 2019-04-05 17:15:34 +02:00
src/Avalonia.ThemeManager Code clean-up 2019-04-05 17:12:32 +02:00
tests/Avalonia.ThemeManager.UnitTests Added unit tests stub 2019-04-04 15:07:39 +02:00
.editorconfig Initial import 2019-04-04 13:27:58 +02:00
.gitattributes Initial import 2019-04-04 13:27:58 +02:00
.gitignore Initial import 2019-04-04 13:27:58 +02:00
.nuke Initial import 2019-04-04 13:27:58 +02:00
Avalonia.ThemeManager.sln Update Avalonia.ThemeManager.sln 2019-04-05 17:15:45 +02:00
LICENSE.TXT Initial import 2019-04-04 13:27:58 +02:00
NuGet.Config Initial import 2019-04-04 13:27:58 +02:00
README.md Refactored initialization 2019-04-05 12:19:41 +02:00
build.ps1 Initial import 2019-04-04 13:27:58 +02:00
build.sh Initial import 2019-04-04 13:27:58 +02:00
global.json Initial import 2019-04-04 13:27:58 +02:00

README.md

Avalonia Theme Manager

Build status

NuGet

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 IThemeSelector 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.

License

Avalonia.ThemeManager is licensed under the MIT license.