2012-09-27 26 views
0

我正在创建一个Windows Phone(7.5)应用程序,我需要根据选择哪个主题(黑暗或光明)来更改某些样式。如何在Windows Phone中应用主题特定的样式?

这个应用程序不够好,只是试图找到一种适用于这两种情况的颜色。

这其中的一些在我知道的代码来完成,但是我希望把它在XAML,因为这是真的只是标记/造型,我不希望在我的C#代码:)

这里的我想在伪代码来完成:

<ImageBrush x:Key="BackgroundImageBrush" ApplyForTheme="Dark" Stretch="None" 
ImageSource="/WindowsFanDkApp;component/Content/AppBackground.jpg"/> 

    <ImageBrush x:Key="BackgroundImageBrush" ApplyForTheme="Light" Stretch="None" 
ImageSource="/WindowsFanDkApp;component/Content/AnotherAppBackground.jpg"/> 

似乎无法找到如何做到这一点的任何资源......所以恐怕是不可能的:(

回答

0

您可以搜索在应用程序资源中输入特定的密钥,然后更改图像刷的uri。

这里是提示:

var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"]; 
var theBrush = new ImageBrush(); 
var imageUri = new Uri(isLightTheme == Visibility.Visible ? "bg-light.jpg" : "bg-dark.jpg", UriKind.Relative); 
theBrush .ImageSource = new System.Windows.Media.Imaging.BitmapImage(imageUri); 

编辑: 如何在启动时

public App() 
{ 
    ... 
    //dynamic load style 
    LoadDictionary(); 
    ...  
} 

private void LoadDictionary() 
{ 
    var dictionaries = Resources.MergedDictionaries; 
    dictionaries.Clear(); 
    string source = String.Format("/MyProject;component/DarkStyles.xaml"); 
    var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) }; 
    dictionaries.Add(themeStyles); 
} 
+0

然后你如何应用“theBrush”的资源,所以它在整个应用程序中使用? –

+0

'theBrush'就是一个例子。第一行代码很重要。您可以使用ResourceDictionary/MergedDictionaries在启动时加载样式。请参阅编辑 – Cybermaxs

+0

它无法正常工作,例如我为textblock创建了主题(设置FontSize和Foreground)。当我应用主题时,我看到前景不会改变,但应用了FontSize。有什么建议么? –

0

加载风格Cyber​​maxs建议,您可以使用资源字典/ MergedDictionaries。这里是如何做到这一点与XAML:

<Application.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="TestStyles.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
相关问题