AvaloniaSettingsFactory.Core 1.2.6

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package AvaloniaSettingsFactory.Core --version 1.2.6                
NuGet\Install-Package AvaloniaSettingsFactory.Core -Version 1.2.6                
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="AvaloniaSettingsFactory.Core" Version="1.2.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AvaloniaSettingsFactory.Core --version 1.2.6                
#r "nuget: AvaloniaSettingsFactory.Core, 1.2.6"                
#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 AvaloniaSettingsFactory.Core as a Cake Addin
#addin nuget:?package=AvaloniaSettingsFactory.Core&version=1.2.6

// Install AvaloniaSettingsFactory.Core as a Cake Tool
#tool nuget:?package=AvaloniaSettingsFactory.Core&version=1.2.6                

Avalonia SettingsFactory

NuGet NuGet

Avalonia SettingsFactory is a dynamic UI library that lets you seamlessly implement a settings editor in your Avalonia application using an existing settings object.

https://user-images.githubusercontent.com/80713508/195053696-49c2be25-ef9a-49ae-ab6e-263a6c53f59a.mp4

Usage

Avalonia SettingsFactory works by reading a decorated settings object and sending the results into a custom view that inherits the SettingsFactory UserControl included in this library.

The SettingsFactory component can be initialized with the InitializeSettingsFactory function and an optional SettingsFactoryOptions instance to configure the front-end actions used by the internal library.

public partial class SettingsView : SettingsFactory, ISettingsValidator

// ...

InitializeComponent();

// ...

SettingsFactoryOptions options = new() {

    // Application implementation of a message prompt
    AlertAction = (msg) => Debug.WriteLine(msg),

    // Folder browse dialog or custom input system.
    BrowseAction = async (title) => {
        OpenFolderDialog dialog = new() { Title = title };
        var result = await dialog.ShowAsync(App.StaticView);
        return result;
    },
};

// Custom resource loader (must always return a Dictionary<string, string>)
FetchResource = (res) => JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(res))

// Implement custom logic to occur after saving or cancelling
AfterSaveEvent += () => {
    // Dispose view after saving
    (App.StaticView.DataContext as AppViewModel).Content = null;
};

AfterCancelEvent += () => {
    // Dispose view after cancelling
    (App.StaticView.DataContext as AppViewModel).Content = null;
};

// Initialize the settings layout
InitializeSettingsFactory(
    new SettingsFactoryViewModel(canCancel: true), // Build-in or custom ViewModel inheriting SettingsFactoryViewModel
    this, // implemented ISettingsValidator
    Settings.Config, // Your decorated settings object
    options
);

The attached view must also have some assosiated XAML to tell the SettingsFactory where to place the setting elements.

  • <StackPanel Name="Root"> | This will hold navigation buttons and folders.
  • <Button Name="Save"> | This button will trigger the Save event.
  • <Button Name="Cancel"> | This button will trigger the Cancel event.
  • <ContentControl Content="{Binding ActiveElement}"> | This will hold the active settings page.

For a basic fluent design page, check out the demo application — SettingsView.axaml

<br>

Each setting (property) can also be validated with custom logic using the ISettingsValidator interface and SettingsFactory indexing.

// Custom validation for specified settings
// in your base settings object
public bool? ValidateString(string key, string value)
{
    return key switch
    {
        // Execute a custom check.
        "UserName" => value.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')),

        // You can also reference other properties using the SettingsFactory indexing.
        "FullName" => value.Contains((string)this["FirstName"]!) && value.Contains((string)this["LastName"]!),
        
        // Run a custom action (e.g. change the application theme)
        "Theme" => ValidateTheme(value),

        // Or return null (default, blank color)
        _ => null
    };
}

// Validate bool properties
public bool? ValidateBool(string key, bool value) { /* ... */ }

// Check all the properties before saving
// and warn the user about specific settings
public string? ValidateSave(Dictionary<string, bool?> validated)
{
    // Singled-out property logic
    if (validated["UserName"] == null) {
        return "Please enter a username before saving!";
    }

    // Optionally do one final check on all properties and return a generic error.
    // If all property values validated true, return null to continue saving.
    return validated.Where(x => x.Value == false).Any() ? "One or more settings could not be verified. Please review your settings." : null;
}

<br>

* Note: Setting properties without a Setting attribute will not be read!

* Check out the Demo application for a complete implementation.

Install

Install with NuGet or build from source.

NuGet
Install-Package AvaloniaSettingsFactory
Install-Package AvaloniaSettingsFactory.Core
Build from Source
git clone https://github.com/ArchLeaders/Avalonia.SettingsFactory.git
dotnet build Avalonia.SettingsFactory

© 2022 Arch Leaders

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AvaloniaSettingsFactory.Core:

Package Downloads
AvaloniaSettingsFactory

Dynamic UI factory that lets you seemlessly implement a dynamic settings page in your AvaloniaUI-11 application.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
11.0.0-rc1.1 105 6/4/2023
11.0.0-preview6 343 5/3/2023
11.0.0-preview5 200 2/12/2023
1.2.6 534 12/25/2022
1.2.5 381 12/25/2022
1.2.4 356 12/23/2022
1.2.3 549 10/30/2022
1.2.2 1,798 10/11/2022