.NET MAUI – JSON file opens from path in iOS debug build but not Mac Catalyst debug build

huangapple go评论69阅读模式
英文:

.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:

&lt;!-- Raw Assets (also remove the &quot;Resources\Raw&quot; prefix) --&gt;
 &lt;MauiAsset Include=&quot;Resources\Raw\**&quot; LogicalName=&quot;%(RecursiveDir)%(Filename)%(Extension)&quot; /&gt;

	&lt;ItemGroup&gt;
	  &lt;MauiAsset Update=&quot;Resources\Raw\ClientSecrets.json&quot;&gt;
	    &lt;LogicalName&gt;ClientSecrets.json&lt;/LogicalName&gt;
	    &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
	  &lt;/MauiAsset&gt;
	&lt;/ItemGroup&gt;

The code that I'm accessing the file with looks like this:

private GoogleCredential GetCredentialsFromFile()
		{
			GoogleCredential credential;

			using(var stream = new FileStream($&quot;ClientSecrets.json&quot;, 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 &#39;/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&#39;.

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&lt;GoogleCredential&gt; 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($&quot;ClientSecrets.json&quot;).Result;

		return credential;
	}

The build is now working for iOS and Mac-Catalyst.

huangapple
  • 本文由 发表于 2023年2月24日 08:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定