Correct way to redraw an application after changing resources. #18400
-
Greetings! static readonly ResourceDictionary myResourceDictionaryMain = new ResourceDictionary();
private static void UpdateResourceDictionary(string theme)
{
var application = Application.Current;
if (!application.Resources.MergedDictionaries.Contains(myResourceDictionaryMain))
application.Resources.MergedDictionaries.Add(myResourceDictionaryMain);
var resourceDic = new ResourceDictionary();
var builder = new ResourceDictionaryBuilder(resourceDic);
var color = theme.ToLower() == "first" ? Colors.Red : Colors.Blue;
builder.Add("FilledButtonBackground", new SolidColorBrush() { Color = color });
myResourceDictionaryMain.ThemeDictionaries["Light"] = resourceDic;
} This should change the color of the button that is pressed. It changes, however, only when I click on background and resize the window. So my question is what would be the correct way in Uno to do that? (I'm using target Desktop if it's matter) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @ArtiDi, AFAIK, I didn't test it, this is more of as WinUI question, but to implement theme changes at runtime without requiring a window resize or page reload, you need to handle the resource dictionary updates dynamically and trigger UI invalidation. Update your RD as you did and use Let me know, if this works/helps. Thank you. |
Beta Was this translation helpful? Give feedback.
Hey @ArtiDi,
AFAIK, I didn't test it, this is more of as WinUI question, but to implement theme changes at runtime without requiring a window resize or page reload, you need to handle the resource dictionary updates dynamically and trigger UI invalidation. Update your RD as you did and use
Application.Current.Resources.MergedDictionaries.Clear()
to ensure that the theme update applies immediately. You can try usingFrameworkElement.RequestedTheme = ElementTheme.Default
to force all controls to reapply the current theme.Let me know, if this works/helps. Thank you.