title: Silverlight 3 Out of Browser - Restart "application"
body: <p>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.</p>
  
  <p>So we can certainly have a selection UI for the themes, but then we need to restart the application to apply the themes.</p>
  
  <p>Obviously, in a browser, this would be easy - we just drop to HtmlPage and inject some JavaScript.</p>
  
  <p>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.</p>
  
  <p>(Have searched around for this and no-one seems to address this point)</p>
  
  <p><strong>UPDATE 1 (Thanks Valeri)</strong></p>
  
  <p>We've applied Valeri's code but are getting problems. We think that the theme may only be settable the once. We have:</p>
  
  <ul>
  <li>Moved the XAML out into a new UserControl (LayoutMockup)</li>
  <li>Set the RootVisual to a Grid and added the MainPage to the Grid in App_Init</li>
  </ul>
  
  <p>On our MainPage, we have (Class1 is our imaginitively titled theme):</p>
  
  <pre><code>public MainPage()
      {
          InitializeComponent();
          this.InitializeUI();
          Class1 customTheme = new Class1();
          customTheme.Source = new Uri("/Migturbo_Spring;Component/Themes/MyGeneric.xaml", UriKind.Relative);
          ChangeTheme(customTheme);
  
  
      }
  </code></pre>
  
  <p>and also the further code:</p>
  
  <pre><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());
      } 
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>System.Exception was unhandled by user
    code   Message="<strong>Error HRESULT E_FAIL
    has been returned from a call to a COM
    component.</strong>"   StackTrace:
           at MS.Internal.XcpImports.CheckHResult(UInt32
    hr)
           at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper
    obj, DependencyProperty property,
    String s)
    ...... etc ......</p>
  </blockquote>
  
  <p>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. :)</p>
  
  <p>Either approach would be great.</p>