2014-09-23 57 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