.Net Maui | App Configuration Settings | All Environment (Dev | Prod )

preview_player
Показать описание
.Net Maui | App Configuration Settings | All Environment (Dev | Prod )

.Net Maui | App Configuration Settings

.Net Maui | Add Form | Add Operation

.Net Maui | Consume Api

.Net Maui | URI base Navigation with model | Binding Listdetails page

.Net Maui | URI base Navigation with parameter | Binding Listdetails page

.Net Maui | List Binding | MVVM advance feature

list binding github repo

.net maui | .net multi platform app | window | android | IOS

android emulator setup

Windows Subsystem for Android| Winodows 11 | .Net Maui App

.Net Maui
App Configuration Settings
All Environment development and proudction
Build Action - Embedded resource
Copy to Output Directory - Copy if newer
add packages
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.Json
Inject Iconfiguration in ViewModel Class
now you can get appsettings value direclly by key or
create class and GetRequiredSection we able to get all key and value.

.NET MAUI is right around the corner and Preview 13 is out right now! There are so many great features packed into .NET MAUI and my favorite has to be all of the great Dependency Injection and configuration that goes into the MauiProgram startup.

Next, let's create a few classes that represent these configuration settings anywhere in the project:

We will now need to configure our json file with a few Microsoft.Extensions.Configuration NuGet packages. Added the following into your .csproj:

Now, let's read in our json file, and add it to our builder.Configuration in our CreateMauiApp method after we create the builder:

he code above reads in the manifest resource from the assembly into a stream. You will want to change the MauiApp to whatever your assembly name is for your project. After that we load it up and add our new configuration to the builder's main ConfigurationManager. We do this so we can dependency inject our IConfiguration anywhere we want.

viewmodel file

first approch to get
string apiBaseUri = _configuration["ApiUri"];

second approch
ApiConfigurationSetting apiConfigurationSetting = _configuration.GetRequiredSection("ApiSettings")

var builder = MauiApp.CreateBuilder();

var getAssemebly = Assembly.GetExecutingAssembly();

using var stream = getAssemebly.GetManifestResourceStream(appsettingfile);

var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();

builder.Configuration.AddConfiguration(config);

{
"ApiSettings": {
"Testuri": "debug mode environment"
}
}

{
"ApiSettings": {
"Testuri": "development"
}
}

{
"ApiSettings": {
"Testuri": "production"
}
}
Рекомендации по теме