C# 如何强制windows phone 8应用程序在灯光主题下运行

C# 如何强制windows phone 8应用程序在灯光主题下运行,c#,windows-phone-8,windows-phone-8-emulator,C#,Windows Phone 8,Windows Phone 8 Emulator,我已经开发了一个windows phone应用程序,我希望该应用程序能够在light主题下运行,而不管用户设置了什么。意味着有没有办法为windows phone 8应用程序设置默认主题 来自 (经过测试和验证;获取主题;从资源中复制以防止将来无法访问) 回答 当用户界面是专门为黑暗主题设计的时候,它在光明主题上看起来不太好,反之亦然 为了防止这种情况,应用程序可以强制使用默认的暗主题或亮主题 在应用程序类的构造函数中,放置以下代码以强制暗主题: if ((Visibility)Applicat

我已经开发了一个windows phone应用程序,我希望该应用程序能够在light主题下运行,而不管用户设置了什么。意味着有没有办法为windows phone 8应用程序设置默认主题

来自

(经过测试和验证;获取主题;从资源中复制以防止将来无法访问)

回答

当用户界面是专门为黑暗主题设计的时候,它在光明主题上看起来不太好,反之亦然

为了防止这种情况,应用程序可以强制使用默认的暗主题或亮主题

在应用程序类的构造函数中,放置以下代码以强制暗主题:

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。

您可以使用Jeff Wilcox的

将它添加到您的项目中(有可用的)并从
App()
constructor调用它

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

您可以找到用法示例。

在应用程序构造函数中调用此方法

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);
    }

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

Source:

行中的异常:var-themmestyles=new-ResourceDictionary{Source=new-Uri(Source,UriKind.Relative)};您是否将
LightStyles.xaml
文件添加到Themes文件夹(如答案所述)?但这就像是为了得到结果而做的安排。是否有任何全局解决方案?此处需要哪个nuget软件包?是否单击了链接?您将直接导航到程序包页面。很遗憾,windows phone 8.1不支持该页面。对于SL for 8.1应用程序,您只需获取源代码并进行编译即可。对于8.1应用程序,请查看此
public App()
    {
        this.RequestedTheme = ApplicationTheme.Light;

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