如何在 .NET MAUI 中加載 json 文件?

引言:

按.NET core傳統方式添加 AddJsonFile("appsettings.json") 在windows平臺和ssr工作正常,但是在 ios 和 android 無法用這種方式,因為資源生成方式不一樣. 使用內置資源方式不夠靈活而且 ios 平臺會提示不能復制 json 文件到目錄,于是進行了幾天的研究,終于能正確使用了.

bec587fbc798ec003e5215854a5d3770.png

資源文件夾

  1. 官方工程?Resources\Raw\文件夾?AboutAssets.txt?文件說明

您希望與應用程序一起部署的任何原始資產都可以放置在此目錄(和子目錄)。將資產部署到您的應用程序, 由 `.csproj` 中的以下 `MauiAsset` 構建操作自動處理。<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />這些文件將與您的包一起部署,并且可以使用 Essentials 訪問:async Task LoadMauiAsset(){using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt");using var reader = new StreamReader(stream);var contents = reader.ReadToEnd();}

復制一份txt文件按操作復現成功.

  1. 直接丟入 appsettings.json 編譯到ios平臺提示錯誤不能復制 json 文件到目錄, 經google,找到方案,需要項目文件屬性中 Remove 文件?<Content Remove="appsettings.json" />

相關錯誤提示

The path 'XXXXXXX\appsettings.json' would result in a file outside of the app bundle and cannot be used.

The path '..\..\..\..\..\..\..\Repos\BlazorMaui\BlazorMaui\appsettings.json' would result in a file outside of the app bundle and cannot be used.

最終方案:

  • appsettings.json文件直接放工程根目錄

  • 文件屬性生成操作為 MauiAsset 和 不復制

  • 需要在項目屬性中 Remove 文件

cb46bf168b78a034cfa33a5b70be5d8d.png

項目文件

<ItemGroup><Content Remove="appsettings.json" /></ItemGroup><ItemGroup><MauiAsset Include="appsettings.json"><CopyToOutputDirectory>Never</CopyToOutputDirectory></MauiAsset></ItemGroup>

讀取配置文件代碼

async static Task<Stream> LoadMauiAsset(){try{using var stream = await FileSystem.OpenAppPackageFileAsync("appsettings.json");using var reader = new StreamReader(stream);var contents = reader.ReadToEnd();Console.WriteLine("OpenAppPackageFileAsync => " + contents);return stream;}catch (Exception e){Console.WriteLine("OpenAppPackageFileAsync Exception => " + e.Message);}return null;}

附加到 builder.Configuration

var stream = LoadMauiAsset().Result; 
builder.Configuration.AddJsonStream(stream);

附:使用內置資源方式

需要在項目屬性中設置生成操作為嵌入資源

<ItemGroup><EmbeddedResource Include="appsettings.json" />
</ItemGroup>

代碼?BlazorMaui?為工程名

var a = Assembly.GetExecutingAssembly();
using var stream = a.GetManifestResourceStream("BlazorMaui.appsettings.json");
builder.Configuration.AddJsonStream(stream);

項目地址

https://github.com/densen2014/BlazorMaui

https://gitee.com/densen2014/BlazorMaui

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281122.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281122.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281122.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

onlyOfice取消上傳文件大小的限制

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/757 使用onlyOfice的時候&#xff0c;在打開的文件中&#xff0c;對文件的大小有限制的&#xff0c;可以在服務中修改被限制的大小&#xff0c;在服務上有/etc/onlyoffice/documentserver/default.json的文件&a…

SSH整合注解版(Spring+Struts2+Hibernate)

整體架構&#xff1a; pom.xml 引入maven節點&#xff1a; <dependencies><!--單測--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.3</version><scope>test</scope><…

定時插座動一下就斷_使用插座定時器在某些時候自動將您的Amazon Echo靜音

定時插座動一下就斷The Amazon Echo is an always-listening voice-controlled virtual assistant, but if there are times you’d rather not listen (or be listened to) by the Echo, here’s how to automatically mute it at certain times of the day. Amazon Echo是一個…

周末讀書:《紅樓夢》

【周末讀書】| 作者/Edison大家好&#xff0c;我是Edison。古人曾說“開談不說紅樓夢&#xff0c;讀盡詩書也枉然”&#xff0c;剛好最近我爸開始在閱讀《紅樓夢》&#xff0c;我想起當年看了兩遍《紅樓夢》原著和一遍87版《紅樓夢》電視劇的場景。本文是我首發于2018年的一篇讀…

onlyoffice啟用HTTPS

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/765 HTTPS需要使用SSL證書&#xff0c;可以自己簽發也可以用ca機構簽發的&#xff0c;加密效果相同。 生成證書&#xff1a; 創建私鑰 openssl genrsa -out onlyoffice.key 2048 創建CSR openssl req -new -k…

Oracle-邏輯體系結構

這里指數據文件的邏輯體系結構&#xff0c;包括1.表空間(TABLESPACE) 2.段(SEGMENT) 3.區(EXTENT) 4.塊(BLOCK) 數據庫(Database)由若干表空間(TABLESPACE)組成&#xff0c;表空間由若干段(SEGMENT)組成&#xff0c;段由若干區(EXTENT)組成&#xff0c;區由若干塊(BLOCK)組成…

win10下用docker安裝onlyoffice服務

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/699 1. 使用DockerToolbox安裝docker 1.1 DockerToolbox下載地址 DockerToolbox-19.03.1 GitHub上下載實在是太慢了。 我找了好久終于下載下來了&#xff0c;在這里分享一下&#xff01; 網盤下載&#xff1…

chromebook刷機_如何從Chromebook上的APK側面加載Android應用

chromebook刷機Chromebooks can now download and install Android apps from Google Play, and it works pretty well. But not every Android app is available in Google Play. Some apps are available from outside Google Play as APK files, and you can install them o…

速度和性能狂卷,.NET 7來了

.NET 作為一個免費的跨平臺開放源代碼開發人員平臺&#xff0c;這些年在不斷的升級完善。就在最近&#xff0c;史上最快最強的.net平臺.NET 7于2022年11月8日正式發布, .NET 朝著更好的???邁進了?步&#xff01;那么&#xff0c;.NET 7 有什么新東西&#xff1f;.NET 7 建立…

前端JavaScript規范

摘要&#xff1a; JavaScript規范 目錄 類型 對象 數組 字符串 函數 屬性 變量 條件表達式和等號 塊 注釋 空白 逗號 分號 類型轉換 命名約定 存取器 構造器 事件 模塊 jQuery ES5 兼容性 HTML、CSS、JavaScript分離 使用jsHint 前端工具 類型 原始值: 相當于傳值(JavaScript對…

修改onlyoffice存儲為手動存儲關閉瀏覽器時不進行保存

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/704 相關官方API地址&#xff1a; 文件保存 回調處理程序 配置-編輯-定制-自動保存 配置-編輯-定制-forcesave 需要將: config.editorConfig.customization.forcesave改為true, 并且config.editorConfig.…

如何使用NVIDIA ShadowPlay錄制PC游戲

NVIDIA’s ShadowPlay, now known as NVIDIA Share, offers easy gameplay recording, live streaming, and even an FPS counter overlay. It can automatically record gameplay in the background–just on the PlayStation 4 and Xbox One–or only record gameplay when y…

.Net 7 的 R2R,Crossgen2是什么?

楔子來下這些概念R22,Crossgen2這兩個東西&#xff0c;跟前面講的AOT和CLR有異曲同工之妙&#xff0c;到底什么呢&#xff1f;本篇來看下。R2RR2R(ReadyToRun),是一種結合了AOT和CLR編譯模式&#xff0c;取其優點&#xff0c;拋其缺點的一種編譯方式。具體的呢&#xff0c;R2R包…

Java流

流分類 字節流字符流輸入流InputStreamReader輸出流OutputStream WriterInputStream:BufferedInputStream、DataInputStream、ObjectInputStreamOutputStream:BufferedOutputStream、DataOutputStream、ObjectOutputStream、PrintStream 標準流: System.in 、Syst…

Win7安裝OnlyOffice(不使用Docker)

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/741 1、安裝準備 &#xff08;1&#xff09;安裝Elang&#xff1a; 【注意事項】 a)Elang是為了給RabbitMQ使用的&#xff0c;因此在安裝Elang之前應確定RabbitMQ的版本及其所需的Elang版本。RabbitMQ的地址…

geek_享受How-To Geek用戶樣式腳本的好處

geekMost people may not be aware of it but there are two user style scripts that have been created just for use with the How-To Geek website. If you are curious then join us as we look at these two scripts at work. 大多數人可能不知道它&#xff0c;但是已經創…

.NET Core統一參數校驗、異常處理、結果返回

我們開發接口時&#xff0c;一般都會涉及到參數校驗、異常處理、封裝結果返回等處理。如果每個后端開發在參數校驗、異常處理等都是各寫各的&#xff0c;沒有統一處理的話&#xff0c;代碼就不優雅&#xff0c;也不容易維護。所以&#xff0c;我們需要統一校驗參數&#xff0c;…

Memcached 在linux上安裝筆記

第一種yum 方式安裝 Memcached 支持許多平臺&#xff1a;Linux、FreeBSD、Solaris、Mac OS&#xff0c;也可以安裝在Windows上。 第一步 Linux系統安裝memcached&#xff0c;首先要先安裝libevent庫 Ubuntu/Debian sudo apt-get install libevent libevent-deve 自動下…

onlyoffice回調函數controller方式實現

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/706 springboot實現的onlyoffice協同編輯網盤項目可以去作者博客。 上代碼&#xff1a; //新建報告GetMapping("report/createReport")public String CreatReport(HttpServletRequest request,Stri…

讀Bilgin Ibryam 新作 《Dapr 是一種10倍數 平臺》

Bilgin Ibryam 最近加入了開發者軟件初創公司Diagrid Inc&#xff0c;他是Apache Software Foundation 的 committer 和成員。他也是一個開源的布道師&#xff0c;并且是書籍 Kubernetes設計模式 和 Camel Design Patterns 的作者。早在2020年初 提出的Multi-Runtime Microserv…