Sandreas.Avalonia.Preferences 0.0.3

dotnet add package Sandreas.Avalonia.Preferences --version 0.0.3                
NuGet\Install-Package Sandreas.Avalonia.Preferences -Version 0.0.3                
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="Sandreas.Avalonia.Preferences" Version="0.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sandreas.Avalonia.Preferences --version 0.0.3                
#r "nuget: Sandreas.Avalonia.Preferences, 0.0.3"                
#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 Sandreas.Avalonia.Preferences as a Cake Addin
#addin nuget:?package=Sandreas.Avalonia.Preferences&version=0.0.3

// Install Sandreas.Avalonia.Preferences as a Cake Tool
#tool nuget:?package=Sandreas.Avalonia.Preferences&version=0.0.3                

Avalonia.Preferences

Cross platform preferences library for AvaloniaUI

Usage

  • Install nuget Sandreas.Avalonia.Preferences

Dependency Injection

var services = new ServiceCollection();
// ...        
services.AddSingleton<Preferences>();
// ...        

API sample (simple)


var counter = 0;

// check for key
if (preferences.ContainsKey("counter"))
{
    // get value with defaultValue fallback
    counter = _preferences.Get("counter", 0);
}


counter++;

// set value and check for success
if(!_preferences.Set("counter", counter)) {
    Console.WriteLine("Error: Could not set counter");
}

// remove value
if(!_preferences.Remove("counter")) {
    Console.WriteLine("Error: Could not remove counter");
}

// remove all values (clear)
var clearedItemsCount = _preferences.Clear();
if(clearedItemsCount == -1) {
    Console.WriteLine("Error: Could not clear preferences");
} else {
    Console.WriteLine("Success: Removed " + clearedItemsCount + " items from preferences");
}

API sample (async, xplat)

Platform specific storage

If you need platform specific storage (e.g. for iOS or Android), you have to implement your own IPreferences implementation and statically set it before instantiating Preferences. To simplify the implementation, you can extend AbstractPreferencesStorage already providing some helpful overridable methods. As an example take a look at GenericPreferencesStorage

    // e.g. YourProject.Android/SplashActivity.cs
    protected override void OnResume()
    {
        base.OnResume();
        // your platform specific IPreferences implementation must be added statically before instantiation
        Preferences.PlatformStorage = new AndroidPlatformStorage(Application.Context);
        StartActivity(new Intent(Application.Context, typeof(MainActivity)));
    }
Async usage
// _preferences is set as class property via Dependency Injection
private async Task<int> GetCounterAsync() {    
    // cancellation is not actually used, but you could
    var cts = new CancellationTokenSource(); 
    var ct =  cts.Token;
    var counter = 0;

    // check for key
    if (_preferences.ContainsKey("counter"))
    {
        // get value with defaultValue fallback
        counter = await _preferences.GetAsync("counter", 0, ct);
    }


    counter++;

    // set value and check for success
    if(!await _preferences.SetAsync("counter", counter, ct)) {
        Console.WriteLine("Error: Could not set counter");
    }

    // remove value
    if(!await _preferences.RemoveAsync("counter", ct)) {
        Console.WriteLine("Error: Could not remove counter");
    }

    // remove all values (clear)
    var clearedItemsCount = await _preferences.ClearAsync(ct);
    if(clearedItemsCount == -1) {
        Console.WriteLine("Error: Could not clear preferences");
    } else {
        Console.WriteLine("Success: Removed " + clearedItemsCount + " items from preferences");
    }
    return counter;
}
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.
  • net6.0

    • No dependencies.

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.0.3 1,161 3/23/2023
0.0.2 230 3/22/2023
0.0.1 224 3/21/2023