microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.85k stars 323 forks source link

Missing Content files if inserted in a Class Library project #4621

Closed fabianoriccardi closed 3 weeks ago

fabianoriccardi commented 3 months ago

Describe the bug

I would embed few files as Content in a Class Library project. Theese files such be used only by the Library. However, they seem to be ignored, because they are missing when I try to load them. If I move these files in the main project (a regular Blank App, Packaged (WinUI 3 in Desktop)) they can be loaded using the usual ms-appx:///Assets/.... path.

For now, I solved including these file as Embedded Resource, but I still wonder if this is an intended behavior or a bug.

Steps to reproduce the bug

Create an empty Class Library and a Blank App. Reference the ClassLibrary (Class1) into the BlackApp (App1). Insert a content file (e.g. Class1/Assets/square.png).

try to load the file from the Class1:

public class1()
{
   Task.Run(async () =>
   {
      Uri uri = new($"ms-appx:///Assets/square.png");
      StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
   });
}

It will throw a not found exception. But, if I move the square.png to App1/Assets/square.png, the image is properly loaded.

Expected behavior

I would be able to load Content files located in a ClassLibrary.

Screenshots

No response

NuGet package version

Windows App SDK 1.5.5: 1.5.240627000

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 22H2 (19045, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

whiskhub commented 3 months ago

You will need to reference it via ms-appx:///ClassLibrary/Assets/square.png

fabianoriccardi commented 3 months ago

I will try it as soon as possible! May you link where this is explained on Microsoft docs?

whiskhub commented 3 months ago

I can't find any documentation about it either... 😅I agree this should be mentioned somewhere.

I found out by trial and error (if you check the build output directory you can see a ClassLibrary\Assets folder, which exactly matches the URI path.

fabianoriccardi commented 3 months ago

I confirm it works, thanks!

I hope the maintainers will add a mention about it in the official docs.

alvinashcraft commented 1 month ago

Hi @fabianoriccardi and @whiskhub. Thanks for raising the concern about this not being mentioned in the docs anywhere. I'll be adding a section to this page on the Windows dev docs on Microsoft Learn this week. Look for a new section about resources in libraries on this page soon: https://learn.microsoft.com/windows/apps/windows-app-sdk/mrtcore/images-tailored-for-scale-theme-contrast

If you ever see anything missing or incorrect in the docs, we love feedback. There's a Feedback button (thumbs up/down) on each page that opens a popup where you can describe the issue. Many pages also have a link at the bottom of the page to submit GitHub Issues against the docs. PRs are always welcome too (click the pencil icon to begin an edit on a page)! :)