You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI 9预览版Blazor Hybrid Web App中FileSystem无法使用问题

解决.NET MAUI Blazor Hybrid Web端FileSystem未实现错误

问题核心:Microsoft.Maui.Storage.FileSystem是.NET MAUI专属API,仅在MAUI平台(安卓、iOS、Mac等)有实现,Blazor Hybrid的Web端没有对应的平台实现,因此运行时会抛出NotImplementedInReferenceAssemblyException。

解决方案:抽象接口+平台专属实现

通过定义统一的文件访问接口,分别实现MAUI和Web端的文件读取逻辑,让业务代码与平台细节解耦。

1. 定义抽象服务接口

在共享项目中创建统一的文件访问接口:

public interface IFileAccessService
{
    Task<ObservableCollection<T>> ReadJsonDataAsync<T>();
}

2. 实现MAUI端文件服务

在MAUI项目中实现接口,沿用原有的FileSystem逻辑:

using Microsoft.Maui.Storage;
using System.Collections.ObjectModel;
using System.Text.Json;

public class MauiFileAccessService : IFileAccessService
{
    private readonly string _jsonFileName = "Resources/Raw/languages.json";

    public async Task<ObservableCollection<T>> ReadJsonDataAsync<T>()
    {
        var result = new ObservableCollection<T>();
        
        if (await FileSystem.Current.AppPackageFileExistsAsync(_jsonFileName))
        {
            using var stream = await FileSystem.Current.OpenAppPackageFileAsync(_jsonFileName);
            using var reader = new StreamReader(stream);
            var jsonContent = await reader.ReadToEndAsync();
            
            result = JsonSerializer.Deserialize<ObservableCollection<T>>(jsonContent) ?? result;
        }

        return result;
    }
}

3. 实现Web端文件服务

在Blazor Web项目中实现接口,使用HttpClient读取wwwroot下的静态文件(注意Web端文件要放在wwwroot/Resources/Raw目录下):

using System.Collections.ObjectModel;
using System.Text.Json;

public class WebFileAccessService : IFileAccessService
{
    private readonly HttpClient _httpClient;
    private readonly string _jsonFilePath = "Resources/Raw/languages.json";

    public WebFileAccessService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task<ObservableCollection<T>> ReadJsonDataAsync<T>()
    {
        var result = new ObservableCollection<T>();
        
        try
        {
            var jsonContent = await _httpClient.GetStringAsync(_jsonFilePath);
            result = JsonSerializer.Deserialize<ObservableCollection<T>>(jsonContent) ?? result;
        }
        catch (HttpRequestException)
        {
            // 文件不存在时返回空集合
        }

        return result;
    }
}

4. 注册平台专属服务

  • MAUI项目(MauiProgram.cs):
builder.Services.AddSingleton<IFileAccessService, MauiFileAccessService>();
  • Web项目(Program.cs):
builder.Services.AddScoped<IFileAccessService, WebFileAccessService>();

5. 在Blazor组件中使用服务

在MainLayout.razor.cs中注入接口并调用:

public partial class MainLayout
{
    [Inject]
    private IFileAccessService FileAccessService { get; set; } = null!;

    protected override async Task OnInitializedAsync()
    {
        var languageData = await FileAccessService.ReadJsonDataAsync<YourLanguageModel>();
        // 将数据绑定到页面展示
    }
}

注意事项

  • Web端确保languages.json文件位于wwwroot/Resources/Raw,并设置文件属性的复制到输出目录为“如果较新则复制”。
  • 避免在共享代码中直接引用Microsoft.Maui.Storage.FileSystem,通过抽象接口隔离平台差异,保证代码跨平台兼容性。

内容的提问来源于stack exchange,提问作者wafers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:38:16