Use qualifiers in image file names does not work in UWP runtime

Abu Raihan 21 Reputation points
2020-07-12T13:44:33.327+00:00

I was trying to change image for Theme/HighContrast change in UWP app. I followed the link: tailor-resources-lang-scale-contrast But it is not working when I change theme while app is running. It works after app restarting. I followed folder name qualifiers & file name qualifiers both. Do I need to do any additional changes? Can anyone please help me?

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Fay Wang - MSFT 5,211 Reputation points
    2020-07-13T07:02:29.933+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Based on this document, it mentions you need to subscribe the MapChanged event on ResourceContext.QualifierValues to respond to changes in system settings. When your HighContrast changes, this event will be triggered, then you can reload your images with the help of the default ResourceContext. For example:

    .cs:

    public MainPage()  
    {  
        this.InitializeComponent();  
    
          
        // Subscribe to the event that's raised when a qualifier value changes.  
        var qualifierValues = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues;  
        qualifierValues.MapChanged += new Windows.Foundation.Collections.MapChangedEventHandler<string, string>(QualifierValues_MapChanged);  
    }  
    
    private async void QualifierValues_MapChanged(IObservableMap<string, string> sender, IMapChangedEventArgs<string> @event)  
    {  
        var dispatcher = this.myImageXAMLElement.Dispatcher;  
        if (dispatcher.HasThreadAccess)  
        {  
            this.RefreshUIImages();  
        }  
        else  
        {  
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => this.RefreshUIImages());  
        }  
    }  
    
    private void RefreshUIImages()  
    {  
        //The folder naming: \Assets\Images\contrast-standard\1.jpg  
        var namedResource = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap[@"Files/Assets/Images/1.jpg"];  
        this.myImageXAMLElement.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(namedResource.Uri);  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.