2013-07-11 48 views
8

我的應用程序通常被設計爲dark主題,我使用StaticResources因此,如果用戶改變了手機的主題light應用程序變得不可讀,無法使用。的Windows手機8如何始終思考着一個問題,即使手機的主題改變

我試圖改變每個元件的手動顏色和避免使用StaticResources之類的東西:

Style="{StaticResource PhoneTextLargeStyle}" 

StaticResources用於字體和顏色。但這是一項艱苦的工作。

如何在全球範圍內將主題更改爲我的應用程序認爲手機主題是黑暗的? (這是一款Windows Phone 8應用程序)

回答

20

更新: 由於的Windows Phone 8.1的發佈,你可以設置任何控制RequestedTheme屬性,甚至在應用層面覆蓋用戶在設置中設置的主題。

實施例以迫使光主題:

在代碼,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> 

For the old Windows Phone 8應用程序模型:

當然,在設計準則中建議使用主題資源以確保您的應用程序能夠使用任何主題和口音顏色。

但是,如果你真的想迫使黑暗的主題,這裏是魯迪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構造函數中,檢查燈光主題是否啓用,如果是,你重寫主題:

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

感謝電話8.1參考。我需要做RequestedTheme = ApplicationTheme.Light;但是也要將rootFrame的背景設置爲白色。這是因爲我的大部分網頁都沒有設置背景。 – Quincy

+1

這不會在WP8上將背景顏色變成黑色。文字是白色的,但背景仍然是白色的。我爲'PhoneBackgroundBrush'嘗試了一些值,包括'Colors.Black'和'Color.FromArgb(0xFE,0x01,0x01,0x01);',但沒有運氣。 – ruffin

12

我只是想推薦Jeff Wilcox的'Windows Phone Theme Manager'nuget軟件包,它可以輕鬆實現光線和黑暗主題的這種功能。

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

函數調用只需添加到應用程序的構造函數:

ThemeManager.ToDarkTheme(); 

ThemeManager.ToLightTheme(); 
+1

更多的運氣與此。你甚至不需要NuGet它;它非常友善,包含[單個文件(Apache許可)](https://github.com/jeffwilcox/wp-thememanager/blob/master/src/JeffWilcox.PhoneThemeManager/ThemeManager.cs),您可以將其投入到您的項目中。 – ruffin

+0

小心,這不適用於WinRT版本(即8.1+) – ericosg