dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

`<MauiAsset>` is hard to use #3270

Closed mattleibow closed 2 years ago

mattleibow commented 2 years ago

Description

I added a <MauiAsset Include="Resources\Assets\*"> in my project and I was unable to easily find how to retrieve it. I used Essentials' FileSystem.OpenAppPackageFileAsync and I found that it went to different folders:

mattleibow commented 2 years ago

@Eilon I think whatever we do is going to affect Blazor. I think we need to get that #791 change in. Just looking at your issues, I see there are a few around this.

We probablu need somehting for Blazor (that can convert to a MauiAsset under the hood with special values), but right now we need something way better than what we have.

Eilon commented 2 years ago

@mattleibow maybe we should have a chat about MauiAssets and whatever we'd like to change/improve there?

IIARROWS commented 2 years ago

So... does anyone know a way to access assets? I would like to just point to a file on a known location and opening it, I think that should be common code.

Note: I'm using Blazor, I don't have experience with Xamarin but I have with UWP.

limefrogyank commented 2 years ago

@IIARROWS I may be "doing it wrong" but for UWP, MauiAssets doesn't work. I have to use Content type build action, then use Maui.Essentials.FileSystem.OpenAppPackageFileAsync and a relative path to the file.

For Android, I must use MauiAsset build action and the same package.

This seems broken to me.

limefrogyank commented 2 years ago

Guess I was doing it wrong. With UWP, you should set your build action to MauiAsset then:

await Microsoft.Maui.Essentials.FileSystem.OpenAppPackageFileAsync("Assets/{same path in app });

Better yet:

#if WINDOWS
        var stream = await Microsoft.Maui.Essentials.FileSystem.OpenAppPackageFileAsync("Assets/" + filePath);
#elif ANDROID
        var stream = await Microsoft.Maui.Essentials.FileSystem.OpenAppPackageFileAsync(filePath);
#endif
Eilon commented 2 years ago

Hi folks, we definitely have some work to do to make consuming MauiAssets easier.

Here's the "rough" code that we have in BlazorWebView for each platform (Android, iOS/MacCatalyst, and WinUI):

But of course we should just make this a lot easier to use. But hopefully this unblocks people in the meantime.

Eilon commented 2 years ago

Notes from meeting with @mattleibow :

  1. Remove "Assets" special folder from Windows targets in Resizetizer to make it consistent with other platforms
  2. Consider updating project template to have MauiAsset glob a. Add glob to template: b. Add a sample raw resource "txt" file to "Resources/Raw/asset.txt" --> the content of this file can show the code to read it using Essentials.OpenPackageFileAsync() c. Fix the bug where LogicalName doesn't work on some platforms
  3. Remove "Assets" path combine workaround from BlazorWebViewHandler on Windows: src\BlazorWebView\src\Maui\Windows\WinUIWebViewManager.cs
  4. Fix up any samples in the MAUI repo to be more consistent with how we expect MauiAssets to be used