2014-09-23 29 views
0

在我的Windows Phone 8应用程序,我有主题设置的页面如下图所示:如何更改主题,只要在WP8 C#listpicker选择值

enter image description here

在listpicker控制我有两个值DarkLight 。当我从listpicker控制选择Dark我想改变应用程序的主题:

,并在themelistPicker1_SelectionChanged方法,我做象下面这样:

private void themelistPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem; 
      themename = lpi.Content.ToString(); 
      if (themename == "Dark") 
      { 
       App.dark(); 
      } 
      else 
      { 
       App.light(); 
      } 
     } 

,并在App.xaml.cs页我已经定义Dark并定义像下面Light方法:

using System; 
using System.Diagnostics; 
using System.Resources; 
using System.Windows; 
using System.Windows.Markup; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Shell; 
using GetContacts.Resources; 
using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Windows.Media; 

namespace GetContacts 
{ 
    public partial class App : Application 
    { 
     /// <summary> 
     /// Provides easy access to the root frame of the Phone Application. 
     /// </summary> 
     /// <returns>The root frame of the Phone Application.</returns> 
     /// <summary> 
     /// Constructor for the Application object. 
     /// </summary> 
     public App() 
     { 
      // Global handler for uncaught exceptions. 
      UnhandledException += Application_UnhandledException; 

      // Standard XAML initialization 
      InitializeComponent(); 

      // Phone-specific initialization 
      InitializePhoneApplication(); 
      // Language display initialization 
      InitializeLanguage(); 

      // Show graphics profiling information while debugging. 
      if (Debugger.IsAttached) 
      { 
       // Display the current frame rate counters. 
       Application.Current.Host.Settings.EnableFrameRateCounter = true; 

       // Show the areas of the app that are being redrawn in each frame. 
       //Application.Current.Host.Settings.EnableRedrawRegions = true; 

       // Enable non-production analysis visualization mode, 
       // which shows areas of a page that are handed off to GPU with a colored overlay. 
       //Application.Current.Host.Settings.EnableCacheVisualization = true; 

       // Prevent the screen from turning off while under the debugger by disabling 
       // the application's idle detection. 
       // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run 
       // and consume battery power when the user is not using the phone. 
       PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; 
      } 
     } 
     public static void dark() 
     { 
      (App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color = Color.FromArgb(12, 12, 54, 145); 
      (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.White; 
      (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.Gray; 
     } 
     public static void light() 
     { 
      (App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color = Color.FromArgb(12, 12, 54, 145); 
      (App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.Black; 
      (App.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color = Colors.White; 
     } 

    } 
} 

但是,当我选择Dark主题PhoneBackgroundBrush=Black,而不是GrayPhoneForegroundBrush=white以及当我选择Light主题PhoneBackgroundBrush=Black而不是WhitePhoneForegroundBrush=Black

请问我应该怎么做,在从listpicker控件选择值后更改应用程序主题,你有什么想法吗?等待回复。

谢谢

+0

你经历这个? http://msdn.microsoft.com/en-us/library/windows/apps/ff769545(v=vs.105).aspx – Kulasangar 2014-09-23 12:25:07

回答

1

我有个主意。使用ThemeManager开发者Jeff Wilcox 通过使用ThemeManager,您可以更改应用程序的主题。

中的NuGet包安装ThemeManager图书馆&更改代码这样的..

private void themelistPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem; 
      themename = lpi.Content.ToString(); 
      if (themename == "Dark") 
      { 
       ThemeManager.ToDarkTheme(); 
      } 
      else 
      { 
       ThemeManager.ToLightTheme(); 
      } 
     } 

希望这有助于

+0

主题经理工作正常,但是当我选择'黑暗'主题'PhoneBackgroundBrush =黑色'而不是'灰色'。为什么?并且它对整个应用程序没有影响。 – user88 2014-09-23 06:51:41

+0

只需探索thememanager类来定制主题。有一个方法'Setbackground'。你可以尝试像这样... 'ThemeManager.SetBackground(新的SolidColorBrush(Colors.Gray));'' – Kumar 2014-09-23 07:03:57