Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML WinRT - Factory Pattern for Custom Styles

I'd like to implement a sort of Factory Pattern for XAML. I created an app for WinRT where I defined two xaml style files. Basically, what I'd like to achieve (if possible) is to load one of the the two xaml file when the application starts. in the solution explorer I have this:

enter image description here

CustomStyles foder contains the style files. So, based on an enumerator in my App.xaml.cs file

public enum Style
{
    Style_1,
    Style_2
}

if I choose Style_1 I'd like to load the xaml file Style_1.xaml else Style_2.xaml at run-time. Both the style files, have the same definition of Button style, TextBlock style, etc with different property values. Here an example:

Style_1.xaml

<Style x:Key="Attribute_Label" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="Foreground" Value="#78CAB3" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="FontWeight" Value="Normal" />
</Style>

Style_2.xaml

<Style x:Key="Attribute_Label" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="Foreground" Value="#606060" />
    <Setter Property="FontSize" Value="30" />
    <Setter Property="FontWeight" Value="Normal" />
</Style>

There is a way to achieve what I want to do ? Thank you in advance.

like image 915
spaghettifunk Avatar asked Dec 18 '13 16:12

spaghettifunk


1 Answers

We end up to do something like this:

  1. defining a ResourcesDictionary in App.xaml with all the CustomStyles

  2. we make a server request that decides which custom style has to be loaded

  3. using this piece of code Application.Current.Resources[CustomStyleVariable]; we load the whole style in a Style object.

We haven't found any better solution yet but, it seems to work.

like image 109
spaghettifunk Avatar answered Nov 19 '22 13:11

spaghettifunk