我们正在为我们的后台产品开发一个相当复杂的 Silverlight 3 RIA UI。其部分功能是用户可以选择主题。我们正在使用 Telerik 主题,这些要求我们在 App_Init 时应用主题选择。
所以我们当然可以有一个主题的选择 UI,但是我们需要重新启动应用程序来应用主题。
显然,在浏览器中,这很容易——我们只需拖放到 HtmlPage 并注入一些 JavaScript。
但是 Out of browser 应用程序呢?另一个要求是一旦 OOB 检测到并下载了应用程序的更新版本。
(已经搜索过这个,似乎没有人解决这个问题)
更新 1(感谢瓦莱里)
我们已经应用了 Valeri 的代码,但遇到了问题。我们认为主题可能只能设置一次。我们有:
- 将 XAML 移出到新的 UserControl (LayoutMockup)
- 将 RootVisual 设置为 Grid 并将 MainPage 添加到 App_Init 中的 Grid
在我们的 MainPage 上,我们有(Class1 是我们富有想象力的主题):
public MainPage()
{
InitializeComponent();
this.InitializeUI();
Class1 customTheme = new Class1();
customTheme.Source = new Uri("/Migturbo_Spring;Component/Themes/MyGeneric.xaml", UriKind.Relative);
ChangeTheme(customTheme);
}
以及进一步的代码:
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());
}
第一次运行时,它可以工作。“Spring/Class1”主题已正确应用。第二次(由 UI 上的模拟按钮启动)使用已知的工作主题调用 ChangeTheme() 方法,我们得到一个异常:
System.Exception 未被用户代码处理 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 属性、String s) ......等等......
我们选择了重新启动应用程序而不是切换主题的路线,因为我们在某处读到这是不可能的。但我们是 Silverlight 的新手,很高兴接受教育。:)
任何一种方法都会很棒。