Windows phone 8即使手机的主题发生变化,如何始终在一个主题上

时间:2013-07-11 00:09:46

标签: c# windows-phone-7 xaml windows-phone-8

我的应用通常是针对dark主题而设计的,我正在使用StaticResources因此,如果用户将其手机的主题更改为light,该应用就会变得不可读且无法使用。

我尝试手动更改每个元素的颜色,并避免使用StaticResources等内容:

Style="{StaticResource PhoneTextLargeStyle}"

StaticResources表示字体和颜色。但这是一项艰苦的工作。

如何将主题全局更改为我的应用认为手机主题是黑暗的? (这是一款Windows Phone 8应用程序)

2 个答案:

答案 0 :(得分:20)

更新: 自 Windows Phone 8.1 发布以来,您可以在任何控件上设置 RequestedTheme 属性,甚至可以在应用级别设置覆盖用户设置的主题设置

强制使用Light主题的示例:

在代码中,在App类的构造函数中:

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public sealed partial class App : Application
{
    private TransitionCollection transitions;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.RequestedTheme = ApplicationTheme.Light;

        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
    }
}

或XAML中的

<Application
    x:Class="App26.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Light"
    xmlns:local="using:App26">
</Application>

对于旧的 Windows Phone 8 应用程序模型:

当然,在设计指南中建议使用主题资源来确保您的应用适用于任何主题和强调色。

然而,如果你真的想强迫黑暗的主题,这里是Rudy Huyn在他的博客上提供的解决方案:http://www.rudyhuyn.com/blog/2013/01/18/forcer-un-theme-sous-windows-phone-8/

我们的想法是在你的App类中添加一个方法,该方法将覆盖所有具有黑暗主题颜色的系统画笔:

private void DarkTheme()
{
    ((SolidColorBrush)Resources["PhoneRadioCheckBoxCheckBrush"]).Color = ((SolidColorBrush)Resources["PhoneRadioCheckBoxBorderBrush"]).Color = ((SolidColorBrush)Resources["PhoneForegroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneContrastForegroundBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneContrastBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneDisabledBrush"]).Color = Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneProgressBarBackgroundBrush"]).Color = Color.FromArgb(0x19, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextCaretBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneTextBoxBrush"]).Color = Color.FromArgb(0xBF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneTextBoxEditBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextBoxReadOnlyBrush"]).Color = Color.FromArgb(0x77, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneSubtleBrush"]).Color = Color.FromArgb(0x99, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextBoxSelectionForegroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneButtonBasePressedForegroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextHighContrastBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextMidContrastBrush"]).Color = Color.FromArgb(0x99, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneTextLowContrastBrush"]).Color = Color.FromArgb(0x73, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneSemitransparentBrush"]).Color = Color.FromArgb(0xAA, 0x00, 0x00, 0x00);
    ((SolidColorBrush)Resources["PhoneChromeBrush"]).Color = Color.FromArgb(0xFF, 0x1F, 0x1F, 0x1F);

    ((SolidColorBrush)Resources["PhoneInactiveBrush"]).Color = Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneInverseInactiveBrush"]).Color = Color.FromArgb(0xFF, 0xCC, 0xCC, 0xCC);
    ((SolidColorBrush)Resources["PhoneInverseBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
    ((SolidColorBrush)Resources["PhoneBorderBrush"]).Color = Color.FromArgb(0xBF, 0xFF, 0xFF, 0xFF);
}

然后,在App构造函数中,检查Light主题是否已启用,如果是,则覆盖主题:

if ((Visibility) Resources["PhoneLightThemeVisibility"] == Visibility.Visible)
{
    DarkTheme();
}

答案 1 :(得分:12)

我只想推荐Jeff Wilcox的“Windows Phone Theme Manager”nuget软件包作为实现此功能的简单方法,适用于明暗主题。

http://www.nuget.org/packages/PhoneThemeManager/

只需向App构造函数添加一个函数调用:

ThemeManager.ToDarkTheme();

ThemeManager.ToLightTheme();