2013-06-20 57 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}" />