locked
Bug? RequestedTheme for element in 8.1 "bleeds" across pages

    Question

  • I'm setting RequestedTheme="Light" for a single element (TextBox) on a page in my app (because it always appears on a white background). It seems to work fine in the dark theme UNTIL I navigate away from the page that included that element. Now ALL the page backgrounds and many of the other controls on them have taken on the light theme.

    I noticed this first with a flyout: see http://stackoverflow.com/questions/25529624/flyout-changes-pages-theme.

    Am I doing something wrong?  the feature seems unusable at the moment.  This is using Prism under the covers and was originally a Windows 8 app, so not sure if there's something buried in the framework that's to blame?

    Monday, February 02, 2015 11:56 PM

All replies

  • Hi Jim,

                 I don't think that a themeresource is intended to be used on a single element, but across the entire app.  Let's look at this documentation:

    By using ThemeResource, the default XAML controls force a re-evaluation of all those resources if the user happens to switch themes while the app remains running, so controls, apps and the system will all be using matching theming.

    We need to read between the lines - when you changed the theme for the TextBox, it actually determined that you wanted to change the theme for the entire app so that the entire app would appear consistent.

    If you need to change the look of a specific control, it would be better to just change the color rather than rely on the themeresource.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, February 03, 2015 1:00 PM
    Moderator
  • But then what would be the use case for FrameworkElement.RequestedTheme?

    Changing the RequestedTheme value is effectively changing the resource lookup behavior for the element's default template.  If you change the value to Light then the template uses the values from the ResourceDictionary that is keyed as "Light" in the ThemeDictionaries collection. Setting the UI theme differently from the app's theme is often appropriate for floating controls such as menus and flyouts.

    I think I see what you meant by reading between the lines, but "floating controls such as menus and flyouts" is pretty much the use case I have.  Those controls would certainly have subelements, like TextBlocks that are used elsewhere in the app (where the theme change should NOT be applied)

    FWIW, as a work around I tried forcing RequestedTheme on the page back to the application theme in OnNavigatedTo and it had no effect.  The page said it was using the Dark (well, Default) theme, but it showed using the light theme.  It's as if the ThemeResources have been hijacked, rather than the change applying just to the element on which the property was applied.

    Certainly, I can create an entirely new style for the TextBox that uses the "light" theme colors directly, but that seems like an anti-pattern, and the scenario for which RequestedTheme was designed for in the first place?

    Tuesday, February 03, 2015 1:50 PM
  • I think you're right.  Can you provide a small project so I can file a bug using it?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, February 03, 2015 3:02 PM
    Moderator
  • I'm not seeing it happen with the 8.1 sample https://code.msdn.microsoft.com/windowsapps/XAML-requested-theme-sample-e6d46def so will try to see where my app differs in its usage.  If I manage to reproduce it, I'll send you the modded 8.1 sample as the repro.
    Tuesday, February 03, 2015 5:17 PM