AvaloniaSettingsFactory 0.1.11

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

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

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 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
);

<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 net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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.

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
11.0.0-rc1.1 88 6/4/2023
11.0.0-preview6 286 5/3/2023
11.0.0-preview5 153 2/12/2023
1.2.6 431 12/25/2022
1.2.5 327 12/25/2022
1.2.4 325 12/23/2022
1.2.3 408 10/30/2022
1.2.2 432 10/11/2022
1.2.1 416 10/11/2022
1.2.0 415 10/11/2022
1.1.14 419 10/11/2022
1.1.13 443 10/11/2022
1.1.12 424 10/11/2022
1.0.1 432 10/11/2022
1.0.0 429 10/11/2022
0.1.11 449 10/11/2022
0.1.10 446 10/11/2022