Theme manager for Avalonia applications.
Перейти к файлу
wieslawsoltes 24ac9e6789 Fix build 2019-08-09 07:36:27 +00:00
build Use 0.8.1 2019-08-09 07:17:46 +00:00
samples/AvaloniaApp Fix build 2019-08-09 07:36:27 +00:00
src/Avalonia.ThemeManager Added PackageId to csproj 2019-05-25 21:06:31 +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 Use Microsoft.NETFramework.ReferenceAssemblies 2019-05-04 14:01:26 +02:00
LICENSE.TXT Initial import 2019-04-04 13:27:58 +02:00
NuGet.Config Updated Avalonia 2019-04-23 08:45:54 +02:00
README.md Added badge 2019-04-23 09:20:32 +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 Updated 2019-08-08 10:27:16 +02:00

README.md

Avalonia Theme Manager

Build status

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