WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

WPF Pack URIs don't work in a WPF app referencing Windows App SDK

Open JaykeBird opened this issue 2 years ago • 4 comments

Describe the bug

I was working on a WPF app and wanted to implement the Windows App SDK using it, but when I tried to run the app after referencing the SDK via NuGet, I got an error stating that an image file I was using couldn't be found. The image file was being referenced in the WPF app the recommended way. The issue disappeared once I removed the Windows App SDK reference.

The error I receive is System.IO.FileNotFoundException: 'Could not find file 'C:\Users\jacob\source\repos\WpfPackUriErrorRepro\WpfPackUriErrorRepro\bin\Debug\net6.0-windows10.0.19041.0\SmileyColor.png'.' The image file shouldn't be and isn't separate from the application; instead, it's embedded as a resource in the WPF application and should be referenced via the Pack URI, such as pack://application:,,,/SmileyColor.png.

Steps to reproduce the bug

I created a minimum-code reproduction that features this issue: https://github.com/JaykeBird/WpfPackUriErrorRepro The reproduction should display a star image in the middle of the window and print a line to Visual Studio's Output window about the width of a different image.

If you want to create similar code from scratch yourself:

  1. Create a new WPF Application via Visual Studio's New Project dialog.
  2. Add an image to the newly created project (such as right-clicking the project entry in Solution Explorer, going to "Add > Existing Item", and then finding an image file to add).
  3. With the image file added to the project, right-click it, select "Properties", and then set the "Build Action" to "Resource".
  4. In the application's main window, add an Image control, and set its source to the image file you just added. It should look something like <Image Stretch="None" Source="/myImage.png" />.
  5. Build and run the project. You can see the image appear in the window that appears.
  6. Now, add a reference to Microsoft.WindowsAppSDK to the project via NuGet.
  7. Try to build and run the project again. The project builds but the image does not appear in the window anymore.
  8. In the MainWindow.xaml.cs file, add the line var bim = new BitmapImage(new Uri("pack://application:,,,/myImage.png", UriKind.RelativeOrAbsolute));, replacing the "myImage.png" part with the name of the image file in question.
  9. Try to build and run the project again. The project builds, but an exception is thrown on the line that was just added.

Expected behavior

The WPF application should be able to run, and display resource images without throwing such an exception. This behavior shouldn't change based upon if you have the Windows App SDK referenced or not.

Screenshots

This is the expected result: image

Right now, the star does not appear and instead FileNotFoundExceptions occur.

NuGet package version

Windows App SDK 1.2.3: 1.2.230118.102

Packaging type

Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

I originally encountered this with Windows App SDK version 1.2.221209.1. I did not test this with any of the previous versions, so I do not know if this is new or old.

JaykeBird avatar Feb 07 '23 05:02 JaykeBird

This still occurs with Microsoft.WindowsAppSDK 1.3.230331000. The error appears when this NuGet package is installed and disappears when the package is uninstalled.

skst avatar May 08 '23 18:05 skst

I'm still encountering this bug in version 1.5.240428000 of the Windows App SDK.

I noticed in the changelog a section about trying <ShouldComputeInputPris>true</ShouldComputeInputPris> in the project file, but it didn't seem to make a difference.

JaykeBird avatar May 24 '24 02:05 JaykeBird

Still encountering this with the latest version 1.6.250108002 of the Windows App SDK.

This also occurs if I reference another project or package that references the Windows App SDK, not just a direct reference.

JaykeBird avatar Jan 24 '25 04:01 JaykeBird

I know this is about a year old already, but maybe try disabling MRTCore if you don't use it? <EnableCoreMrtTooling>false</EnableCoreMrtTooling>

dongle-the-gadget avatar Oct 31 '25 11:10 dongle-the-gadget