Silverlight 3 - 动态更改合并字典

时间:2009-12-09 16:12:43

标签: silverlight

您好,如何前往:

<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

1 个答案:

答案 0 :(得分: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]是存在xaml文件的组件的命名空间。

然后将其合并到页面的dictioanry中:

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

        dictionary.MergedDictionaries.Add(rd);