This library is depreciated in favor of https://github.com/ArchLeaders/ConfigFactory
Avalonia SettingsFactory is a dynamic UI library that lets you seamlessly implement a settings editor in your Avalonia application using an existing settings object.
Avalonia.SettingsFactory.Demo.mp4
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 theSave
event.<Button Name="Cancel">
| This button will trigger theCancel
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
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;
}
* Note: Setting properties without a Setting attribute will not be read!
* Check out the Demo application for a complete implementation.
Install with NuGet or build from source.
Install-Package AvaloniaSettingsFactory
Install-Package AvaloniaSettingsFactory.Core
git clone https://github.com/ArchLeaders/Avalonia.SettingsFactory.git
dotnet build Avalonia.SettingsFactory
© 2022 Arch Leaders