Silverlight PRISM and "free" style files

We're looking at starting a new Silverlight project using the PRISM framework (to capitalize on modules, etc.) and I'm still a bit unclear as to the best design approach. Ideally, I would like to have editable XAML files (maybe even one for the entire project) containing the app style so that they can be edited to change the look of the app without having to recompile everything. Is this approach used by humans? I am guessing it will need to load the file on startup and apply the style, which I suppose won't be a significant overhead.

Just wondering what approaches people use

thank you for your time

+2


source to share


2 answers


In Silverlight (and WPF), the typical / normal approach is to store your styles and brushes in a resource dictionary. they can be swapped to change the layout, colors and control patterns at any time. You can include them in the XAP that is generated when you create your application or in a standalone assembly.



Be aware that resource files can be loaded at different levels. Application level down to the level of individual controls. dictionaries loaded at the lower level will take precedence over the higher level. For me, it helps to think of it as layers.

+2


source


Here's my approach -

In your App.xaml, you will want to declare the element MergedDictionaries

like this.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles\Colors.xaml" />
            <ResourceDictionary Source="Styles\Brushes.xaml" />
            <ResourceDictionary Source="Styles\Typeography.xaml" />
            <ResourceDictionary Source="Styles\ModuleAStyles.xaml />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

      

As you can see, I tend to have several separate files here for colors (colors if you wrote them correctly), brushes, typography, and then I usually use one extra for each module. Use more than one if it is a large application, otherwise you will end up with too much stuff in one file and it will be difficult to maintain. You just need to use your best judgment here and see which suits you best.

In the Typeography.xaml that I reference in this combined dictionary, I have declared a style called ApplicationTitle like this ...

<!--Application Title-->
<Style TargetType="{x:Type TextBlock}"
       x:Key="ApplicationTitle">
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="HorizontalAlignment"
            Value="Center" />
    <Setter Property="FontFamily"
            Value="Georgia" />
    <Setter Property="Foreground"
            Value="{StaticResource ResourceKey=FlatGradientLightest}" />
    <Setter Property="FontSize"
            Value="24" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="5"
                              Color="#333"
                              Opacity=".3" />
        </Setter.Value>
    </Setter>
</Style>

      

You will notice that in this style, I again refer to another resource called FlatGradientLightest with a markup extension StaticResource

. This resource exists in the Brushes.xaml file ...

    <!--Flat Diagonal Gradient Lightest-->
<LinearGradientBrush x:Key="FlatDiagonalGradientLightest"
                     StartPoint="0,0"
                     EndPoint="1,1">
    <GradientStop Color="{StaticResource ResourceKey=Light}"
                  Offset="0" />
    <GradientStop Color="{StaticResource ResourceKey=Lightest}"
                  Offset="1" />
</LinearGradientBrush>

      



Again this refers to "Light of Flowers" and "Lightest". They exist in the Colors.xaml file ...

<Color x:Key="Light"
       A="255"
       R="190"
       G="190"
       B="190" />
<Color x:Key="Lightest"
       A="255"
       R="250"
       G="250"
       B="250" />

      

More importantly here, I have defined resource dictionaries in App.xaml. If I were to move the Typeography.xaml to the top of the list, it would trigger runtime, because at the time it loaded this style, its dependencies did not exist.

Since WPF / SL looks up to resolve resources (as Muad'Dib said), you can simply use those resources in your modules as if they were declared locally. So in one of my modules, I declare TextBlock

like this ...

<TextBlock Text="Menu Module Loaded" Style="{StaticResource ResourceKey=ApplicationTitle}" />

      

Now TextBlock

uses the "ApplicationTitle" style in Typeography.xaml, which loads its Foreground

brush from "FlatGradientLightest" LinearGradientBrush

in Brushes.xaml, which in turn loads two colors from the resources Color

in the Colors.xaml file.

Some kind of calm right?

Hope it helps.

+2


source







All Articles