2009-12-09 23 views
0

您好,如何去从:的Silverlight 3 - 更改合并字典动态

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="teste.App"> 
<Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
    **<ResourceDictionary Source="Green.xaml"/>** 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
</Application> 

要:

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="teste.App"> 
<Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
    **<ResourceDictionary Source="Blue.xaml"/>** 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
</Application> 

动态,当用户选择主题...难道可能? 谢谢 Josi

回答

0

你可以改变资源字典动态后面的代码中,在page.xaml的负荷:

应用:

//Load in resources either from application 
     ResourceDictionary dictionary = Application.Current.Resources as ResourceDictionary; 

然后,一旦你拥有的资源字典,你可以使用简单的逻辑来应用它:

//Load in xaml 
     if (user1) 
      themefile = @"[Assembly];component/Themes/blue.xaml"; 
     else 
      themefile = @"[Assembly];component/Themes/green.xaml"; 

注:[Assembly]是n xaml文件存在的组件的amespace。

然后将其合并到dictioanry与页:

XDocument xaml = XDocument.Load(themefile); 
ResourceDictionary rd = XamlReader.Load(xaml.ToString()) as ResourceDictionary; 

     dictionary.MergedDictionaries.Add(rd); 
+0

是否有将它应用到整个应用程序设置新的主题一次呢? – 2009-12-09 16:56:56

+0

XDocument是什么命名空间? – 2009-12-09 17:18:41

+0

XDocument位于System.Linq命名空间中。 – 2009-12-09 17:24:21