2013-06-20 45 views
0

OK,這裏是我的2倍問題:不同的風格和切換主題

A)

在發展,我需要能夠從黑暗開關燈的主題,而無需運行模擬器中的應用 - 這可能嗎?如果是這樣,怎麼樣?

B)

關於顏色/我的各種控制等我通常使用內置標準顏色(如Foreground="{StaticResource PhoneSubtleBrush}")。

現在,如果我想創建自定義樣式和希望 - 讓我們說 - 設置Foreground灰色(同時使用Light主題)和橙紅色(同時採用深色主題) - 應該如何這樣做?

回答

1

答:可以在Visual Studio中使用設計器來查看頁面在不同主題/口音組合下的外觀。使用設備窗口(在「設計」菜單下)。 Blend中也存在類似的選項。

Visual Studio Device Window

B)你可以做到這一點與轉換器,但我喜歡做我自己的資源,這樣的事情。只要創建一個這樣的類:

public class MyColorResource 
{ 
    /// <summary> 
    /// The resource name - as it can be referenced by within the app 
    /// </summary> 
    private const string ResourceName = "MyColorResource"; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="MyColorResource"/> class. 
    /// </summary> 
    public MyColorResource() 
    { 
     try 
     { 
      // This doesn't work in the designer - so don't even try 
      if (DesignerProperties.IsInDesignTool) 
      { 
       return; 
      } 

      // Make sure we don't try and add the resource more than once - would happen if referenced on multiple pages or in app and page(s) 
      if (!Application.Current.Resources.Contains(ResourceName)) 
      { 
       if (Visibility.Visible == (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"]) 
       { 
        Application.Current.Resources.Add(ResourceName, new SolidColorBrush(Colors.Red)); 
       } 
       else 
       { 
        Application.Current.Resources.Add(ResourceName, new SolidColorBrush(Colors.Gray)); 
       } 
      } 
     } 
     catch (Exception exc) 
     { 
      System.Diagnostics.Debug.WriteLine("Something went wrong - ask for your money back"); 
      System.Diagnostics.Debug.WriteLine(exc); 
     } 
    } 
} 

某處在您的應用程序做一個參考,以它(在App.xaml中或您的主網頁通常是好)

<phone:PhoneApplicationPage.Resources> 
    <local:MyColorResource x:Key="AnythingAsNotActuallyUsed" /> 
</phone:PhoneApplicationPage.Resources> 

然後,您可以使用它像任何其他資源一樣的XAML:

<TextBlock Foreground="{StaticResource MyColorResource}" Text="{Binding Name}" />