THIS HAS BEEN MOVED TO THE MAIN SHINY REPOSITORY AT https://github.com/shinyorg/shiny
Release | NuGet |
---|---|
Stable | |
Preview |
- All the power of IConfiguration
- Load JSON from packaged sources (whitelabellers can unpack, edit, & repack the config)
- Preferences based configuration source with writebacks
- Support For
- Xamarin
- iOS
- Android
- Including Xamarin Forms :P
- .NET 6 is coming
- Xamarin
Microsoft really did create a great set of abstractions for configuration. You can store a set of key/values, cause a reload when a configuration source changes, & bind to strongly typed objects using Microsoft.Extensions.Configuration.Binder. On Mobile though, NONE of the current providers aren't really well.
Why?
- A big feature for IConfiguration, is the ability to trigger a reload of it's source without restarting the application. Changing an appsettings.json file during runtime causes the IConfiguration to trigger a reload notification.
- Mobile doesn't really have an appsettings.json. Sure you could put this in an embedded resource, but than it is readonly at all times other than the build process... after that - it is locked in place
- Essentially, IConfiguration becomes a string based dictionary for all intents and purposes - pretty useless considering
How Shiny.Extensions.Configuration brings the power of IConfiguration to Mobile!
- A platform preferences configuration source which allows you to WRITE a value back using IConfiguration[key] = value;
- A whitelabellers dream - the ability to unpack, change a json config file, and repack without trigger a build by using proper platform directories to load up the JSON files while still having the power of a proper configuration library internally.
- Install the NuGet package 'Shiny.Extensions.Configuration'
- Create an appsettings.json file like you would in ASP.NET Core application in your HEAD project.
- ANDROID: place the file in: Assets. Ensure the build action on the file is set to 'AndroidAsset'
- iOS: place the file in the ROOT of your project. Ensure the build action on the file is set to 'BundleResource'
- Configure your IConfiguration using the following code in your application startup code (ie. Xamarin.Forms App).
// store this in your dependency injection container OR static class
var config = new ConfigurationBuilder()
.AddJsonPlatformBundle() // NOTE: you can change the name of appsettings.json to something else and pass as an argument here
.Build();
This provider allows writes and persists across application restarts. It is a wrapper around the Android/iOS shared preferences.
- Install the NuGet package 'Shiny.Extensions.Configuration'
- For this configuration source, nothing special is required, simply add the following to the configuration builder:
var configuration = new ConfigurationBuilder()
.AddPlatformPreferences()
.Build();
NOTE: with the platform preferences provider, changes are supported.
IConfiguration config = ...; // build
config["key"] = "value"; // write - this will cause a persist to the platform prefs and also trigger Option reloated events