We're working on a fairly complex Silverlight 3 RIA UI for our back office product. Part of the functionality of this is that the user may select themes. We're using Telerik themes and these require us to apply the theme selection at the time of App_Init.
So we can certainly have a selection UI for the themes, but then we need to restart the application to apply the themes.
Obviously, in a browser, this would be easy - we just drop to HtmlPage and inject some JavaScript.
But what about an Out of browser application? Another requirement for this is once the OOB has detected and downloaded an updated version of the application.
(Have searched around for this and no-one seems to address this point)
UPDATE 1 (Thanks Valeri)
We've applied Valeri's code but are getting problems. We think that the theme may only be settable the once. We have:
- Moved the XAML out into a new UserControl (LayoutMockup)
- Set the RootVisual to a Grid and added the MainPage to the Grid in App_Init
On our MainPage, we have (Class1 is our imaginitively titled theme):
public MainPage()
{
InitializeComponent();
this.InitializeUI();
Class1 customTheme = new Class1();
customTheme.Source = new Uri("/Migturbo_Spring;Component/Themes/MyGeneric.xaml", UriKind.Relative);
ChangeTheme(customTheme);
}
and also the further code:
public void ChangeTheme(Theme theme)
{
StyleManager.ApplicationTheme = theme; // FAILS HERE 2ND TIME
this.LayoutRoot.Children.Clear();
this.InitializeUI();
}
private void InitializeUI()
{
this.LayoutRoot.Children.Add(new LayoutMockup());
}
The first time this runs, it works. The "Spring/Class1" theme is correctly applied. The second time (initiated by a mock button on the UI) the ChangeTheme() method is called with a known working theme, we get an exception:
System.Exception was unhandled by user code Message="Error HRESULT E_FAIL has been returned from a call to a COM component." StackTrace: at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj, DependencyProperty property, String s) ...... etc ......
We went the route of restarting the application as opposed to switching themes because we had read somewhere that it wasn't possible. But we are new to Silverlight and are happy to be educated. :)
Either approach would be great.