簡體   English   中英

如何強制Windows Phone 8應用程序以輕主題運行

[英]How to force windows phone 8 app to be run in light theme

我已經開發了一個Windows手機應用程序,我希望該應用程序以輕主題運行,無論用戶設置了什么。 意味着有沒有辦法為Windows Phone 8應用程序設置默認主題。

你可以使用Jeff Wilcox的ThemeManager

將它添加到您的項目(有一個可用的NuGet包 )並從App()構造函數中調用它。

public App()
{
    // Global handler for uncaught exceptions.
    UnhandledException += Application_UnhandledException;

    // Standard Silverlight initialization
    InitializeComponent();

    // Phone-specific initialization
    InitializePhoneApplication();

    ThemeManager.ToLightTheme();

    // Other code that might be here already...
}

您可以在他的網站上找到用法示例。

對於Windows Phone 8.1,您可以使用:

<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>

要么

public App()
    {
        this.RequestedTheme = ApplicationTheme.Light;

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

來源: Windows phone 8即使手機的主題發生變化,如何始終使用一個主題

來自http://developergoodies.blogspot.nl/2012/10/force-windows-phone-theme.html

(經過測試和驗證;獲取主題;從資源復制以防止將來無法訪問)

回答

當UI專為黑暗主題設計時,它在光主題上看起來不太好,反之亦然。

為了防止這種情況,應用程序可以強制默認的暗或亮主題。

在應用程序類的構造函數中,使用此代碼強制使用黑暗主題:

if ((Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible) MergeCustomColors("/Themes/DarkStyles.xaml");

或者這段代碼強制輕量級主題:

if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible) MergeCustomColors("/Themes/LightStyles.xaml");

項目中的任何位置都放置此方法:

private void MergeCustomColors(String Theme)
{
    ResourceDictionary Dictionaries = new ResourceDictionary();
    String source = String.Format(Theme);
    var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
    Dictionaries.MergedDictionaries.Add(themeStyles);
    ResourceDictionary appResources = Current.Resources;
    foreach (DictionaryEntry entry in Dictionaries.MergedDictionaries[0])
    {
        SolidColorBrush ColorBrush = entry.Value as SolidColorBrush;
        SolidColorBrush ExistingBrush = appResources[entry.Key] as SolidColorBrush;
        if (ExistingBrush != null && ColorBrush != null) { ExistingBrush.Color = ColorBrush.Color; }
    }
}

該代碼假定項目包含名為Themes的文件夾中的DarkStyles.xaml和LightStyles.xaml文件。

在您的app構造函數中調用此方法

private void LightTheme()
    {
        ((SolidColorBrush)Resources["PhoneRadioCheckBoxCheckBrush"]).Color = ((SolidColorBrush)Resources["PhoneRadioCheckBoxBorderBrush"]).Color = ((SolidColorBrush)Resources["PhoneForegroundBrush"]).Color = Color.FromArgb(0xDE, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
        ((SolidColorBrush)Resources["PhoneContrastForegroundBrush"]).Color = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
        ((SolidColorBrush)Resources["PhoneContrastBackgroundBrush"]).Color = Color.FromArgb(0xDE, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneDisabledBrush"]).Color = Color.FromArgb(0x4D, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneProgressBarBackgroundBrush"]).Color = Color.FromArgb(0x19, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextCaretBrush"]).Color = Color.FromArgb(0xDE, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextBoxBrush"]).Color = Color.FromArgb(0x26, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]).Color = Color.FromArgb(0xDE, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextBoxEditBackgroundBrush"]).Color = Color.FromArgb(0x00, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextBoxReadOnlyBrush"]).Color = Color.FromArgb(0x2E, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneSubtleBrush"]).Color = Color.FromArgb(0x66, 0x00, 0x00, 0x00);
        ((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(0xDE, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextMidContrastBrush"]).Color = Color.FromArgb(0x73, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneTextLowContrastBrush"]).Color = Color.FromArgb(0x40, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneSemitransparentBrush"]).Color = Color.FromArgb(0xAA, 0xFF, 0xFF, 0xFF);

        ((SolidColorBrush)Resources["PhoneInactiveBrush"]).Color = Color.FromArgb(0x33, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneInverseInactiveBrush"]).Color = Color.FromArgb(0xFF, 0xE5, 0xE5, 0xE5);
        ((SolidColorBrush)Resources["PhoneInverseBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0xDD, 0xDD, 0xDD);
        ((SolidColorBrush)Resources["PhoneBorderBrush"]).Color = Color.FromArgb(0x99, 0x00, 0x00, 0x00);
        ((SolidColorBrush)Resources["PhoneAccentBrush"]).Color = Color.FromArgb(0xFF, 0x00, 73, 99);
        ((SolidColorBrush)Resources["PhoneChromeBrush"]).Color = Color.FromArgb(0xFF, 0xDD, 0xDD, 0xDD);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM