Silverlight 3 from the browser - restart the "application"

We are working on a rather sophisticated Silverlight 3 RIA interface for our back office. Part of the functionality of this is that the user can select themes. We use Telerik themes, and this requires us to apply theme selection during App_Init.

So we probably have a user interface for choosing themes, but then we need to restart the application to apply the themes.

Obviously, this will be easy in the browser - we just go to HtmlPage and add JavaScript.

But what about a browser application? Another requirement for this is that OOB has detected and downloaded an updated version of the application.

(Search around this and no one seems to address this issue)

UPDATE 1 (thanks Valerie)

We applied the Valeri code, but there are problems. We believe that a topic can only be set once. We have:

  • Moved XAML to a new UserControl (LayoutMockup)
  • Install RootVisual in the grid and add MainPage to the Grid in App_Init

On our MainPage we have (class1 is our creatively 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); } 

as well as the following 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()); } 

At the first start it works. The theme "Spring / Class1" is correctly applied. The second time (initiated by the mock button in the user interface), the ChangeTheme () method is called with a known working theme, we get an exception:

Correction System.Exception was not handled by the user Code Message = " Error HRESULT E_FAIL was returned from calling the COM component. " StackTrace: in MS.Internal.XcpImports.CheckHResult (UInt32 h) in MS.Internal.XcpImports.SetValue (INativeCoreTypeWrapper obj, property DependencyProperty, String s) ...... etc.

We took the path of restarting the application, as opposed to switching themes, because we read somewhere that this is not possible. But we are not familiar with Silverlight and are happy to be educated. :)

Any approach would be great.

+4
source share
2 answers

Instead of adding the application user interface to the RootVisual control (usually MainPage.xaml), you can add it to a separate UserControl that will be created inside the MainPages code. When you change the theme, you just need to create a new instance of this UserControl and replace the old one. For instance:

 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 a UserControl that contains the application code, and MainPage contains only the Grid, with x: Name = LayoutRoot. When you need to change the theme, you need to call the ChangeTheme method.

Hope this helps.

+3
source

Does the second theme work if you originally installed it?

Unfortunately, I can’t say what the problem is, just looking at the stack trace, you know, SL stack traces sometimes do not provide useful information. I would suggest opening a new support ticket or forum post on telerik.com so that I can send you a working sample. It is also possible that your XAML contains errors, so it would be very helpful if you send us your topics.

0
source

Source: https://habr.com/ru/post/1301102/


All Articles