3
votes

I've created a ResourceDictionary in my Shared project without problem. However, some of my styles will be very particular* to Windows Phone 8.1 and not used in Windows 8.1. As there's no App.xaml in the Windows Phone project, how do I specify a ResourceDictionary just for Windows Phone?

*In addition to different sizes and margins, extensive use of PhoneAccentBrush which is obviously not available on Windows Store Apps.

Or, if the Shared ResourceDictionary is the preferred method, how do I use PhoneAccentBrush and other WP-only resources?

4

4 Answers

1
votes

As there's no App.xaml in the Windows Phone project

This can not be true. Application does not compiles without App.xaml. You may not I noticed it in the Shared project?

App.xaml

I do not see problem to move/copy to each project App.xaml

1
votes

I've been going through the same thing myself. I had all my styles in the Shared project in /Assets/Themes/MySharedStyles.xaml.

The model for Shared content in Universal apps is quite cool. If you have a class or page with the same name (and possibly type) in both projects, then it becomes available in the shared project.

This means that you can put all the shared styles in a file like MySharedStyles.xaml and then in both the Windows and Windows Phone apps create a resource dictionary, '/Assets/MyPlatformSpecificStyles.xaml'.

In your Shared App.xaml you can then do this:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Assets/Themes/MySharedStyles.xaml" />
    <ResourceDictionary Source="Assets/Themes/PlatformSpecificStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

PlatformSpecificStyles will be pulled from the appropriate project based on the Platform DropDown in VS during design time and at runtime.

If you just want to override some specifics in a project, like swapping out a FontFamily on one project or another, you can change the specific font in the MySharedStyles file for FontFamily="{TemplateBinding FontFamily}" then you can simply set it in the places that you use that style in your projects.

These strategies work, I'm using them in an app today.

1
votes

The Windows Phone accent colour is available cross platform as {ThemeResource SystemColorControlAccentColor}, and quoting that link:

This also works in Windows 8.1 apps so that it’s possible to share XAML in universal apps and still take advantage of accent color. But since accent color doesn’t exist on Windows 8.1 the color we get will always be Blueberry.

You may still want to build brushes based on that colour for resources, etc. but these can go in the shared application resources, so something like:

<Application
    x:Class="My.Universal.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:My.Universal">
    <Application.Resources>
        <!-- This will always be Blueberry on Windows 8, but will follow the accent colour on Windows Phone 8 -->
        <SolidColorBrush x:Key="AccentBrush" Color="{ThemeResource SystemColorControlAccentColor}"/>
    </Application.Resources>
</Application>

If you did want to have completely different resources for the different platforms, you can merge the dictionary with one from the same location in both projects - the shared code is "magically" linked to both projects simultaneously

0
votes

Phone specific resources such as PhoneAccentBrush that used to work on a Silverlight 8.0 app will not work in a Universal app.

You can use the same App.xaml file to define your ResourceDictionary, application life cycle, etc.

However, since you want to define Resources specific to the phone you need to have a separate Resource dictionary for Windows Phone 8.1 and Windows 8.1, with the same resource names.