1

For my UWP app, I'm trying to move some assets to my AppData folder. I'm surprised that I've seemed to lose the option of using qualifier names in the process.

For example, for the following pair of images

Assets/Header/header.scale-100.jpg
Assets/Header/header.scale-200.jpg

I used to have the following image source:

<ImageSource x:Key="Header">/Assets/Header/header.jpg</ImageSource>
        

After moving the images to the AppData folder, I'm only seeing the image if I'm explicit about which image I use, like this:

<ImageSource x:Key="Header100">ms-appdata:///local/Assets/Header/header.scale-100.jpg</ImageSource>
<ImageSource x:Key="Header200">ms-appdata:///local/Assets/Header/header.scale-200.jpg</ImageSource>

Then, in order to select the appropriate image, I would have to subscribe to the the QualifierValues.MapChanged event as shown in this answer, and set up my own logic to get the appropriate ImageSource.

I've been looking here and here for some more information, but I cannot see anything that mentions how to use qualifier names in the AppData folder.

Ben Jasperson
  • 300
  • 4
  • 17

1 Answers1

1

How to use resource qualifier names in UWP AppData folder

I have to say you can't use resource qualifier names in UWP AppData folder, the app will not preload resource where in the AppData folder. so it will not work for setting image source with simple file name.

If you do want to use resource qualifier names in UWP AppData folder. we have a workaround that use IValueConverter to return matched filename base on current scale. for getting current scale please refer to ResourceContext document.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • I am using a similar workaround already. I don't understand your explanation for why it's not possible to use qualifier names in the AppData folder. Why does it matter if the file at the path isn't there until runtime? Defining and using the ImageSource works otherwise. Why not the name qualifier? If you can convince me that it really isn't possible (e.g. by citing documentation where that is made explicit), I'll accept your answer. – Ben Jasperson Mar 11 '22 at 16:26
  • 1
    Sure, please refer to this [pink note](https://learn.microsoft.com/en-us/windows/uwp/app-resources/images-tailored-for-scale-theme-contrast#reference-an-image-or-other-asset-from-xaml-markup-and-code) part. *The ms-resource (for string resources) and ms-appx(-web) (for images and other assets) URI schemes perform automatic qualifier matching to find the resource that's most appropriate for the current context. The ms-appdata URI scheme (which is used to load app data) does not perform any such automatic matching,* – Nico Zhu Mar 14 '22 at 01:46