AathifMahir.Maui.MauiTheme 0.5.0

Prefix Reserved
dotnet add package AathifMahir.Maui.MauiTheme --version 0.5.0                
NuGet\Install-Package AathifMahir.Maui.MauiTheme -Version 0.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AathifMahir.Maui.MauiTheme" Version="0.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AathifMahir.Maui.MauiTheme --version 0.5.0                
#r "nuget: AathifMahir.Maui.MauiTheme, 0.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AathifMahir.Maui.MauiTheme as a Cake Addin
#addin nuget:?package=AathifMahir.Maui.MauiTheme&version=0.5.0

// Install AathifMahir.Maui.MauiTheme as a Cake Tool
#tool nuget:?package=AathifMahir.Maui.MauiTheme&version=0.5.0                

Maui Theme

MauiTheme is a theming library that makes theming on .NET MAUI a breeze, providing persistent theme state between sessions and seamless resource swapping, theme switcher, and more.

Get Started

You need to call InitializeTheme() in the App.xaml.cs file as shown in the example below. Ensure that InitializeTheme() is called before setting up the MainPage property.

using MauiTheme.Core;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        Theme.Default.InitializeTheme<App>(x =>
        {
            // Default Theme
            x.DefaultTheme = ThemeMode.Dark;
            // Default Styles Resources
            x.DefaultStyleResources = new List<string> { "Resources/Styles/Styles.xaml" };
            // All Resources Excluding Styles
            x.Resources = new Dictionary<string, string>
            {
                {"Blue", "Resources/Styles/Blue.xaml"},
                {"Purple", "Resources/Styles/Colors.xaml"},
                {"Yellow", "Resources/Styles/Yellow.xaml" },
                {"Green", "Resources/Styles/Green.xaml" }
            };
        });

        MainPage = new AppShell();
    }

App.xaml Setup

The App.xaml should include the default color and style resources as shown below:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
            
                <ResourceDictionary Source="Resources/Styles/Yellow.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

Initialize Properties

Parameters Type Description
DefaultTheme enum The Default Theme, if the Theme is Previously Not Assigned by User
DefaultStyleResources Dictionary<string, string> The Default Style Resources that Needs to Applied with Every Resource Change Eg: Styles.xaml
Resources Dictionary<string, string> All Resources in the Project Excluding Style Resources

Theme

When it comes to switching themes, you can change the CurrentTheme property to switch the theme as shown below:


// Dark
Theme.Default.CurrentTheme = ThemeMode.Dark;

// Light
Theme.Default.CurrentTheme = ThemeMode.Light;

// System
Theme.Default.CurrentTheme = ThemeMode.UnSpecified;

Resources

When it comes to switching resources, you can use the CurrentResource property to swap the resources.

Dislaimer: the resources are applied using the key passed into the InitializeTheme Resources property:


// Blue.xaml
Theme.Default.CurrentResource = "Blue";

// Purple.xaml
Theme.Default.CurrentResource = "Purple";

// Yellow.xaml
Theme.Default.CurrentResource = "Yellow";

Listening to Theme or Resource Changes

This is mainly useful when theme or resource changes are invoked from an external source, such as a razor class library but nothing stopping from using it tradionally:


// Theme Changed Event
Theme.Default.ThemeChanged += (s, t) => 
{
    Debug.Writeline($"New Theme : {t.ToString()}")
}

// Theme Changed Event
Theme.Default.ResourceChanged += (s, r) => 
{
    Debug.Writeline($"New Resource : {r}")
}

Additionally, we can use ICommand as well, those are ThemeChangedCommand and ResourceChangedCommand.

Properties and Methods

Parameters Type Description
InitializeTheme() method This is used for Initializing MauiTheme
CurrentTheme ThemeMode Gets or sets the current theme
CurrentResource string Gets or sets the current resource
ThemeChanged event Theme Changed event is fired whenever theme changes happens
ResourceChanged event Resource Changed event is fired whenever resource changes happens
ThemeChangedCommand ICommand Theme Changed Command is fired whenever theme changes happens
ResourceChangedCommand ICommand Resource Changed Command is fired whenever resource changes happens

License

Maui Theme is Licensed Under MIT License.

Contribute and Credit

Credits to @taublast for Helping with Resource Creation.

If you wish to contribute to this project, please don't hesitate to create an issue or request. Your input and feedback are highly appreciated. Additionally, if you're interested in supporting the project by providing resources or becoming a sponsor, your contributions would be welcomed and instrumental in its continued development and success. Thank you for your interest in contributing to this endeavor.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-android33.0 is compatible.  net7.0-ios was computed.  net7.0-ios16.1 is compatible.  net7.0-maccatalyst was computed.  net7.0-maccatalyst16.1 is compatible.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows10.0.19041 is compatible.  net8.0 is compatible.  net8.0-android was computed.  net8.0-android34.0 is compatible.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-ios17.2 is compatible.  net8.0-maccatalyst was computed.  net8.0-maccatalyst17.2 is compatible.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows10.0.19041 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.0 221 1/21/2024
0.1.1-preview 135 1/16/2024
0.1.0-preview 99 1/16/2024
0.0.1-preview 106 1/8/2024

v0.5.0
• First Stable Build
• Breaking Changes, Check the Docs
https://github.com/AathifMahir/MauiTheme

v0.1.0-preview
• Enhancements and Fixes
• Breaking Changes on Maui Built in AppTheme Construct, Now MauiTheme Uses Custom Construct Called MauiAppTheme

v0.0.1-preview
• Initial Preview Version