How can I change my app's colour scheme from VB code? RRS feed

  • Question

  • Background: my app has 4 colours hard-coded in the App.XAML as 'PrimaryColour', etc. and this works fine.

    I would like to change the way my app stores the colour so the colours can be changed according to values in a separate class (I want to use the app as the basis for a template). That way each app I create from my template can have its own colour scheme and I wouldn't have to modify the App.XAML, just the class that defines to data and code specific to that app. This would make managing the template much simpler.

    For example, my App.XAML has this:

    <SolidColorBrush x:Key="PrimaryColourBrush" Color="White"/>

    and many elements in the UI are set to this colour. 

    At launch time I want to override this property with a colour stored in my data class (ThisTeam), but the following code doesn't work (the class is accessible  - I have confirmed in debugging that at the time of this code the class is loaded and all values can be accessed):

     App.Current.Resources("PrimaryColourBrush") = ThisTeam.TeamPrimaryColour

    (the code in the ThisTeam class is : 

     Public Property TeamPrimaryColour As SolidColorBrush = New SolidColorBrush(Colors.Orange))

    The result should be that all the white elements in the app are displayed as orange; the result I get is a crash with a non-specific error at the time I try to change the colour resource.

    I assume this functionality is possible, and I assume I'm doing it wrong...any ideas?


    Monday, September 17, 2012 12:57 PM


All replies

  • Out of curiosity try this,

    App.Current.Resources("PrimaryColourBrush") = Colors.Orange;

    You see what I am guessing. I may be wrong but I guess so... Let me know and I'll look into this if it doesn't work.

    Monday, September 17, 2012 3:49 PM
  • Thanks, but that doesn't work either.
    Monday, September 17, 2012 3:55 PM
  • <SolidColorBrush x:Key="PhoneBackgroundBrush" Color="{StaticResource PhoneBackgroundColor}"/>

    (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.White;

    Ref: http://blog.johanlaanstra.nl/?p=95

    This may also be helpful,


    • Marked as answer by pumpkinszwan Tuesday, September 18, 2012 12:07 AM
    Monday, September 17, 2012 5:16 PM
  • Thanks Usman, this works!

    My actual (VB) code is now:

     <SolidColorBrush x:Key="PrimaryColourBrush" Color="White"/>
    TryCast(App.Current.Resources("PrimaryColourBrush"), SolidColorBrush).Color = ThisTeam.TeamPrimaryColour

    So the PrimaryColourBrush property is initially white in the XAML, but the code overrides this and makes it orange at launch time, meaning I can store the colours in my ThisTeam class. Exactly as I wanted.

    thank you for your help

    Tuesday, September 18, 2012 12:10 AM