英文:
.NET MAUI - JSON file opens from path in iOS debug build but not Mac Catalyst debug build
问题
以下是您要求的翻译部分:
我有一个JSON文件,我已将其添加到项目的Resources/Raw/文件夹中,并将其标记为MAUI资源。
在CSProj文件中,我有以下内容:
<!-- 原始资源(也删除“Resources\Raw”前缀) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<ItemGroup>
<MauiAsset Update="Resources\Raw\ClientSecrets.json">
<LogicalName>ClientSecrets.json</LogicalName>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</MauiAsset>
</ItemGroup>
我用以下代码访问文件:
private GoogleCredential GetCredentialsFromFile()
{
GoogleCredential credential;
using (var stream = new FileStream($"ClientSecrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential;
}
所有这些在iOS构建上对虚拟和物理设备都有效,但在MacCatalyst构建期间运行时抛出以下错误:
“找不到文件'/Users/kurt/Projects/Dirt_Wain_Compost_Logger/Dirt_Wain_Compost_Logger/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/Dirt_Wain_Compost_Logger.app/ClientSecrets.json'。”
构建成功,错误出现在调试程序时。
我尝试将FileStream函数中的文件路径更改为寻找“Contents/ClientSecrets.json”,这对MacCataylst有效,但然后为iOS设备抛出相同的错误。
有人知道我做错了什么吗?我觉得必须有一种方法使其适用于所有平台,而不需要做异常处理。
谢谢!
英文:
I have a JSON file that I've added to the project's Resources/Raw/ folder and I have it marked as a MAUI Asset.
In the CSProj file I have:
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<ItemGroup>
<MauiAsset Update="Resources\Raw\ClientSecrets.json">
<LogicalName>ClientSecrets.json</LogicalName>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</MauiAsset>
</ItemGroup>
The code that I'm accessing the file with looks like this:
private GoogleCredential GetCredentialsFromFile()
{
GoogleCredential credential;
using(var stream = new FileStream($"ClientSecrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential;
}
All of this works on iOS builds to virtual and physical devices, but the MacCatalyst build throws this error during runtime:
Could not find file '/Users/kurt/Projects/Dirt_Wain_Compost_Logger/Dirt_Wain_Compost_Logger/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/Dirt_Wain_Compost_Logger.app/ClientSecrets.json'.
The build succeeds, the error is when I launch the program while debugging.
I have tried changing the filepath in the FileStream function to look for $"Contents/ClientSecrets.json" and it works for MacCataylst but then throws the same error for the iOS devices.
Does anyone know what I'm doing wrong? I feel like there has to be a way to make it work for all platforms without making exceptions.
Thanks!!
答案1
得分: 1
以下是翻译好的部分:
看起来我之前不知道这里有.NET Maui文件系统助手:
这个方法起到了作用
public async Task<GoogleCredential> ReadTextFile(string filePath)
{
using (Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath))
{
GoogleCredential credential;
credential = GoogleCredential.FromStream(fileStream).CreateScoped(Scopes);
return credential;
};
}
private GoogleCredential GetCredentialsFromFile()
{
GoogleCredential credential = ReadTextFile("ClientSecrets.json").Result;
return credential;
}
现在iOS和Mac-Catalyst的构建已经工作正常。
英文:
Whelp... it looks like I just didn't know about the .NET Maui Filesystem Helpers found here:
This did the trick
public async Task<GoogleCredential> ReadTextFile(string filePath)
{
using (Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath))
{
GoogleCredential credential;
credential = GoogleCredential.FromStream(fileStream).CreateScoped(Scopes);
return credential;
};
}
private GoogleCredential GetCredentialsFromFile()
{
GoogleCredential credential = ReadTextFile($"ClientSecrets.json").Result;
return credential;
}
The build is now working for iOS and Mac-Catalyst.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论