MAUI:appsettings.json 的构建操作是什么以及如何访问 Android 上的文件?

MAUI: What build action for appsettings.json and how to access the file on Android?

我为 MAUI 应用程序创建了一个应用程序设置文件,并使用 .Host.ConfigureAppConfiguration 从 MauiApp.CreateBuilder(); 在构建器上使用 .Host.ConfigureAppConfiguration 将其加载到 IConfiguration 中;我可以在 Windows 中访问文件,但在 运行 和 Android 模拟器中不能。代码:

 var builder = MauiApp.CreateBuilder();
            builder
                .RegisterBlazorMauiWebView()
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                })
                .Host
                .ConfigureAppConfiguration((app, config) =>
                {
#if __ANDROID__
                    // 
                    //var documentsFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
                    config.AddJsonFile( "appsettings.json", optional: false, reloadOnChange: true);
#endif

#if WINDOWS10_0_17763_0_OR_GREATER
                    //
                    Assembly callingAssembly = Assembly.GetEntryAssembly();
                    Version versionRuntime = callingAssembly.GetName().Version;
                    string assemblyLocation = Path.GetDirectoryName(System.AppContext.BaseDirectory); //CallingAssembly.Location
                    var configFile = Path.Combine(assemblyLocation, "appsettings.json");
                    config.AddJsonFile(configFile, optional: false, reloadOnChange: true);
#endif
                });

模型项目是here

有一个未解决的问题 Add support for appsetting.json 将此代码段显示为当前的解决方法:

var builder = MauiApp.CreateBuilder();
        builder
            .RegisterBlazorMauiWebView()
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            })
            .Host
            .ConfigureAppConfiguration((app, config) =>
            {
                var assembly = typeof(App).GetTypeInfo().Assembly;
                config.AddJsonFile(new EmbeddedFileProvider(assembly), "appsettings.json", optional: false, false);
            });

注意 EmbeddedFileProvider 的用法。这适用于构建操作 EmbeddedResource.

从 .NET MAUI Preview 13 中删除了 Host (https://github.com/dotnet/maui/pull/4505)。已接受答案中完全相同的代码可能无法正常工作。
直到 appsettings.json 被 MAUI 支持(我希望 MAUI 团队支持它),我将我的 appsettings.json 做成一个嵌入式资源,然后像这样加载和注入内容。

var builder = MauiApp.CreateBuilder();

var assembly = Assembly.GetExecutingAssembly();

// appsettings.json is located directly under MyNamespace.csproj
using var stream = assembly.GetManifestResourceStream("MyNamespace.appsettings.json");
stream.Position = 0;
var appSettings = System.Text.Json.JsonSerializer.Deserialize<AppSettings>(stream);

builder.Services.AddSingleton(appSettings)

这可能不是加载文件的最佳方式,但它很有效,对我来说已经足够了。
如果有更好的方法,我也很想知道。

参考资料: https://redth.codes/settings-json-files-in-xamarin-apps/

从 MAUI 的 GA 开始:添加 2 个 nuget 包:

  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Configuration.Json

然后在MauiProgram.cs中添加:

using var stream = Assembly.GetExecutingAssembly()
    .GetManifestResourceStream("YourAppName.appsettings.json");
var config = new ConfigurationBuilder().AddJsonStream(stream).Build();
builder.Configuration.AddConfiguration(config);

参考:https://montemagno.com/dotnet-maui-appsettings-json-configuration/