views:

43

answers:

2

I have a Silverlight 3 application containing six custom user controls. I'd like to load the colour scheme for these controls from an external resource.

The code and XAML containing a default colour scheme would be built in the XAP. Then a parameter on the object tag would contain a URL from where alternate colours can be dynamically loaded.

By the way, the Silverlight 3 application theme feature could be used if that's possible but is really overkill. Only colours need to be changed.

Is this possible and how would you recommend to do it?

A: 

Here is how I would do it.

In App.xaml I would define the application Resource dictionary like this:-

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="ColorTable.xaml" />
    </ReourceDictionary>
    <!-- rest of your application resource entries here -->
  </ResourceDictionary>
</Application.Resources>

Now I would place the ColorTable.xaml outside of the XAP in the same folder that the XAP is sited. This doesn't quite meet all your criteria since an external ColorTable is always required. It is possible to flex this somewhat to achieve the full requirement but it'll be quite messy in comparison.

AnthonyWJones
A: 

I would take a look at the technique Corinna Barber uses in these two articles:
http://blogs.msdn.com/corrinab/archive/2009/11/24/9927729.aspx
http://blogs.msdn.com/corrinab/archive/2009/12/02/9931283.aspx

Basically what she does is, at application startup, she creates a bunch of brushes (both solid and gradients) in a binding helper class (hers is called SysColors). Then she simply binds to these brushes, like so: Background="{Binding CalendarGradient, Source={StaticResource SysColors}}"

The main downside to her approach is that you have to write quite a bit of code when creating gradient brushes. And all the different gradient stops would have to be stored independently in your database (or xml or whatever). I'm thinking now that you could probably store your brushes as xaml and just use XamlReader.Load to load the entire brush object at once. That sounds like a better plan to me, but I haven't tried this, I'm just thinking out loud.

In your situation, at application startup, you could easily load your default scheme OR pick up your color values from a WCF service or wherever. You could even implement INotiyPropertyChanged for all the brushes, and thus be able to swap them at runtime. But I guess that might give you bad performance.

Henrik Söderlund