locked
Shared XAML (resources) in Universal Apps (Themes) RRS feed

  • Question

  • Hi,

    I am currently working on a Universal App and I am having some problems with Dialogs and other resources that I would like to share between the Phone and Windows UI. In particular, I am having problems handling the light and dark theme on the phone in conjunction with these resources.

    I found that some things I could do by creating a Colors.xaml for each of the two projects (One for Windows, one for Phone). This file can contain a set of colors. The Windows one has 1 ThemeDictionary (ResourceDictionary inside ResourceDicionary.ThemeDictionaries), called "Default" while the Phone has two of them, "Light" and "Dark".

    I could then create a shared MyStyles resource dictionary, I define just one ThemeDictionary, called Default, which specifies brushes based on the colors from Colors.xaml. It can also specify styles in that dictionary based on the brushes.

    Also, any XAML file can now specify its own ThemeDictionary, labelled Default, for any style and use styles and colors from before. So far so good.

    What about all these pre-specified brushes? PhoneAccentBrush, for example, can only be used in the phone. Would I have to specify an entire button style twice just to use that brush in the Phone (since it's not available for Windows 8.1)?

    Are there any guidelines for how to manage these things in general, and is there a way to get around the problem with device specific brushes?

    Thanks,

    Tomas Hofmann

    Saturday, January 24, 2015 12:00 AM

Answers

  • You can use platform specific resourcedictionaries to hold platform specific resources. Give the file the same name for each platform and you can reference the appropriate one automatically from a shared Xaml file.

    I discuss this in my blog entry Strategies for sharing code in XAML-based Universal apps.

    Don't forget to support HighContrast themes in your ThemeDictionaries.

    If you need more help with this then please provide more clear details on what you're doing and where you're stuck. A minimal repro demonstrating the problem may help.

    • Marked as answer by T Hofmann Tuesday, January 27, 2015 5:01 PM
    Saturday, January 24, 2015 12:26 AM

All replies

  • You can use platform specific resourcedictionaries to hold platform specific resources. Give the file the same name for each platform and you can reference the appropriate one automatically from a shared Xaml file.

    I discuss this in my blog entry Strategies for sharing code in XAML-based Universal apps.

    Don't forget to support HighContrast themes in your ThemeDictionaries.

    If you need more help with this then please provide more clear details on what you're doing and where you're stuck. A minimal repro demonstrating the problem may help.

    • Marked as answer by T Hofmann Tuesday, January 27, 2015 5:01 PM
    Saturday, January 24, 2015 12:26 AM
  • Thanks Rob,

    This was basically what I was looking for. I had already done some of the things, but there were some tips and tricks that I hadn't thought of until I read your post (like creating my own PhoneAccentBrush for Windows so that I could reference it in a shared style.

    Best Regards,

    Tomas Hofmann

    Tuesday, January 27, 2015 5:01 PM