views:

505

answers:

2

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.

+3  A: 

Instead of adding your application UI in the RootVisual control (usually MainPage.xaml) you could add it in a separate UserControl, that will be instantiated inside the MainPage’s code-behind. When you change the theme, you will just need to create a new instance of this UserControl and replace the old one. For example:

public class MainPage : UserControl
{
    public MainPage()
    {
        this.InitializeComponent();
        this.InitializeUI();
    }

    public void ChangeTheme(Theme theme)
    {
        StyleManager.ApplicationTheme = theme;
        this.LayoutRoot.Children.Clear();
        this.InitializeUI();
    }

    private void InitializeUI()
    {
        this.LayoutRoot.Children.Add(new UIRoot());
    }
}

Where UIRoot is the UserControl that contains the application code and MainPage contains only a Grid, with x:Name=LayoutRoot. When the theme has to be changed you only need to call the ChangeTheme method.

I hope this helps.

Valeri Hristov
Thanks a lot, Valeri. We've not had any success with this. Please see updated post, above. I think I recognise you from Telerik. We are using your 2009.3.1405 internal build of your controls for this.
Program.X
A: 

Does the second theme work if you set it initially?

Unfortunately I cannot say what's the exact problem only by looking at the stack trace, you know, the SL stack traces sometimes do not provide helpful info. I would suggest opening a new support ticket or a forum post on telerik.com so I could send you a working sample. It is also possible that your XAML contains bugs, so it would be of great help if you send us your themes.

Valeri Hristov
Ok Valeri, will do.
Program.X
Hi Valeri, no support ticket is needed. Probably after a sleep, I figured the error occurred due to my loading a theme that was not already pre-loaded. I should have loaded the Office_Black theme, rather than Office_Blue.
Program.X