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

106 Views Asked by At

I was trying to change image for Theme/HighContrast change in UWP app. I followed the link: tailor-resources 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?

1

There are 1 best solutions below

0
On

but it is not working when I change theme while app is running. It works after app restarting.

It is by design, derive from official document , we need to listen MapChanged event that will be invoked when the system contrast theme changed. And modify the image source manually.

For example :

public MainPage()
{
    this.InitializeComponent();
    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.MyImage.Dispatcher;
    if (dispatcher.HasThreadAccess)
    {
        this.RefreshUIImages();
    }
    else
    {
        await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => this.RefreshUIImages());
    }
}

private void RefreshUIImages()
{
    var namedResource = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap[@"Files/Assets/Images/logo.jpg"];
    this.MyImage.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(namedResource.Uri);
}