[源码下载]

重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 系统 UI

  • 获取系统的 UI 相关的设置信息
  • 屏幕方向
  • Snap
  • 为 snap 操作和屏幕方向的改变增加动画效果
  • 缩放至不同屏幕
  • 高对比度

示例
1、演示如何获取系统的 UI 相关的设置信息
UI/UISettingsInfo.xaml.cs

  1. /*
  2. * 演示如何获取系统的 UI 相关的设置信息
  3. */
  4.  
  5. using System;
  6. using Windows.UI.ViewManagement;
  7. using Windows.UI.Xaml.Controls;
  8. using Windows.UI.Xaml.Navigation;
  9.  
  10. namespace XamlDemo.UI
  11. {
  12. public sealed partial class UISettingsInfo : Page
  13. {
  14. public UISettingsInfo()
  15. {
  16. this.InitializeComponent();
  17. }
  18.  
  19. protected override void OnNavigatedTo(NavigationEventArgs e)
  20. {
  21. UISettings uiSettings = new UISettings();
  22.  
  23. lblMsg.Text = "AnimationsEnabled: " + uiSettings.AnimationsEnabled; // 是否启用动画
  24. lblMsg.Text += Environment.NewLine;
  25. lblMsg.Text += "CaretBlinkRate: " + uiSettings.CaretBlinkRate; // 输入光标的闪烁速率
  26. lblMsg.Text += Environment.NewLine;
  27.  
  28. /*
  29. * 光标浏览模式(Caret Browsing) - 在页面中会出现一个类似于记事本中的输入光标,用户可以使用键盘(按 Shift 键 + 方向键)来精确地进行页面文字的选择
  30. * IE8 以上可以通过“F7”来打开/关闭光标浏览模式
  31. */
  32. lblMsg.Text += "CaretBrowsingEnabled: " + uiSettings.CaretBrowsingEnabled; // 当前输入光标是否可用于光标浏览模式
  33. lblMsg.Text += Environment.NewLine;
  34.  
  35. lblMsg.Text += "CaretWidth: " + uiSettings.CaretWidth; // 输入光标的宽度
  36. lblMsg.Text += Environment.NewLine;
  37. lblMsg.Text += "CursorSize: " + uiSettings.CursorSize; // 指针的尺寸
  38. lblMsg.Text += Environment.NewLine;
  39. lblMsg.Text += "DoubleClickTime: " + uiSettings.DoubleClickTime; // 捕获双击时,两次单击间的最长时间
  40. lblMsg.Text += Environment.NewLine;
  41. lblMsg.Text += "HandPreference: " + uiSettings.HandPreference; // 用户界面的方向(LeftHanded 或 RightHanded)
  42. lblMsg.Text += Environment.NewLine;
  43. lblMsg.Text += "MessageDuration: " + uiSettings.MessageDuration; // 消息显示的持续时间,单位:秒
  44. lblMsg.Text += Environment.NewLine;
  45. lblMsg.Text += "MouseHoverTime: " + uiSettings.MouseHoverTime; // hover 事件触发之前,指针可以 hover 的时间
  46. lblMsg.Text += Environment.NewLine;
  47. lblMsg.Text += "ScrollBarArrowSize: " + uiSettings.ScrollBarArrowSize; // 当前窗口滚动条的箭头的大小
  48. lblMsg.Text += Environment.NewLine;
  49. lblMsg.Text += "ScrollBarSize: " + uiSettings.ScrollBarSize; // 当前窗口滚动条的大小
  50. lblMsg.Text += Environment.NewLine;
  51. lblMsg.Text += "ScrollBarThumbBoxSize: " + uiSettings.ScrollBarThumbBoxSize; // 当前窗口滚动条 thumb 的大小
  52. lblMsg.Text += Environment.NewLine;
  53.  
  54. // 获取当前系统的相关颜色
  55. lblMsg.Text += "ActiveCaption: " + uiSettings.UIElementColor(UIElementType.ActiveCaption);
  56. lblMsg.Text += Environment.NewLine;
  57. lblMsg.Text += "Background: " + uiSettings.UIElementColor(UIElementType.Background);
  58. lblMsg.Text += Environment.NewLine;
  59. lblMsg.Text += "ButtonFace: " + uiSettings.UIElementColor(UIElementType.ButtonFace);
  60. lblMsg.Text += Environment.NewLine;
  61. lblMsg.Text += "ButtonText: " + uiSettings.UIElementColor(UIElementType.ButtonText);
  62. lblMsg.Text += Environment.NewLine;
  63. lblMsg.Text += "CaptionText: " + uiSettings.UIElementColor(UIElementType.CaptionText);
  64. lblMsg.Text += Environment.NewLine;
  65. lblMsg.Text += "GrayText: " + uiSettings.UIElementColor(UIElementType.GrayText);
  66. lblMsg.Text += Environment.NewLine;
  67. lblMsg.Text += "Highlight: " + uiSettings.UIElementColor(UIElementType.Highlight);
  68. lblMsg.Text += Environment.NewLine;
  69. lblMsg.Text += "HighlightText: " + uiSettings.UIElementColor(UIElementType.HighlightText);
  70. lblMsg.Text += Environment.NewLine;
  71. lblMsg.Text += "Hotlight: " + uiSettings.UIElementColor(UIElementType.Hotlight);
  72. lblMsg.Text += Environment.NewLine;
  73. lblMsg.Text += "InactiveCaption: " + uiSettings.UIElementColor(UIElementType.InactiveCaption);
  74. lblMsg.Text += Environment.NewLine;
  75. lblMsg.Text += "InactiveCaptionText: " + uiSettings.UIElementColor(UIElementType.InactiveCaptionText);
  76. lblMsg.Text += Environment.NewLine;
  77. lblMsg.Text += "Window: " + uiSettings.UIElementColor(UIElementType.Window);
  78. lblMsg.Text += Environment.NewLine;
  79. lblMsg.Text += "WindowText: " + uiSettings.UIElementColor(UIElementType.WindowText);
  80. lblMsg.Text += Environment.NewLine;
  81.  
  82. AccessibilitySettings accessibilitySettings = new AccessibilitySettings();
  83. lblMsg.Text += "是否启用了高对比度模式: " + accessibilitySettings.HighContrast; // 是否启用了高对比度模式
  84. }
  85. }
  86. }

2、演示与“屏幕方向”相关的知识点
UI/ScreenOrientation.xaml

  1. <Page
  2. x:Class="XamlDemo.UI.ScreenOrientation"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="120 0 0 0">
  12.  
  13. <ToggleButton Name="btnLock" Content="锁定当前方向" IsChecked="False" Checked="btnLock_Checked_1" Unchecked="btnLock_Unchecked_1" />
  14.  
  15. <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" />
  16.  
  17. </StackPanel>
  18.  
  19. </Grid>
  20. </Page>

UI/ScreenOrientation.xaml.cs

  1. /*
  2. * 演示与“屏幕方向”相关的知识点
  3. */
  4.  
  5. using System;
  6. using Windows.Graphics.Display;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Navigation;
  10.  
  11. namespace XamlDemo.UI
  12. {
  13. public sealed partial class ScreenOrientation : Page
  14. {
  15. public ScreenOrientation()
  16. {
  17. this.InitializeComponent();
  18. }
  19.  
  20. protected override void OnNavigatedTo(NavigationEventArgs e)
  21. {
  22. // 平板上的“windows”键相对于平板本身的方向
  23. lblMsg.Text = "NativeOrientation: " + DisplayProperties.NativeOrientation.ToString();
  24. lblMsg.Text += Environment.NewLine;
  25.  
  26. // 平板的方向(Windows.Graphics.Display.DisplayOrientations 枚举:None, Landscape, Portrait, LandscapeFlipped, PortraitFlipped)
  27. // 注:Landscape 顺时针转是 Portrait
  28. lblMsg.Text += "CurrentOrientation: " + DisplayProperties.CurrentOrientation.ToString();
  29.  
  30. // DisplayProperties.CurrentOrientation 发生变化时触发的事件
  31. DisplayProperties.OrientationChanged += DisplayProperties_OrientationChanged;
  32. }
  33.  
  34. protected override void OnNavigatedFrom(NavigationEventArgs e)
  35. {
  36. DisplayProperties.OrientationChanged -= DisplayProperties_OrientationChanged;
  37. }
  38.  
  39. void DisplayProperties_OrientationChanged(object sender)
  40. {
  41. lblMsg.Text += Environment.NewLine;
  42. lblMsg.Text += "CurrentOrientation: " + DisplayProperties.CurrentOrientation.ToString();
  43. }
  44.  
  45. private void btnLock_Checked_1(object sender, RoutedEventArgs e)
  46. {
  47. // DisplayProperties.AutoRotationPreferences - 指定当前 app 所支持的方向,即仅允许设备支持指定的方向(模拟器中无效)
  48. // 注:可在 Package.appxmanifest 中指定
  49. DisplayProperties.AutoRotationPreferences = DisplayProperties.CurrentOrientation;
  50. btnLock.Content = "解除方向锁定";
  51. }
  52.  
  53. private void btnLock_Unchecked_1(object sender, RoutedEventArgs e)
  54. {
  55. DisplayProperties.AutoRotationPreferences = DisplayOrientations.None;
  56. btnLock.Content = "锁定当前方向";
  57. }
  58. }
  59. }

3、演示与“snap”相关的知识点
UI/Snap.xaml

  1. <Page
  2. x:Class="XamlDemo.UI.Snap"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="120 0 0 0">
  12.  
  13. <Button Name="btnUnsnap" Content="unsnap" Click="btnUnsnap_Click_1" />
  14.  
  15. <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0">
  16. <Run>snap 可以通过手势操作,也可以通过快捷键:win + .</Run>
  17. </TextBlock>
  18.  
  19. </StackPanel>
  20. </Grid>
  21. </Page>

UI/Snap.xaml.cs

  1. /*
  2. * 演示与“snap”相关的知识点
  3. *
  4. * 注:
  5. * snap 视图的宽度是固定的:320 像素
  6. * 支持 snap 的最小分辨率为 1366 * 768
  7. */
  8.  
  9. using System;
  10. using Windows.UI.Core;
  11. using Windows.UI.ViewManagement;
  12. using Windows.UI.Xaml;
  13. using Windows.UI.Xaml.Controls;
  14.  
  15. namespace XamlDemo.UI
  16. {
  17. public sealed partial class Snap : Page
  18. {
  19. public Snap()
  20. {
  21. this.InitializeComponent();
  22.  
  23. Window.Current.SizeChanged += Current_SizeChanged;
  24.  
  25. ShowCurrentApplicationViewState();
  26. }
  27.  
  28. void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
  29. {
  30. ShowCurrentApplicationViewState();
  31. }
  32.  
  33. void ShowCurrentApplicationViewState()
  34. {
  35. /*
  36. * ApplicationView.Value - 获取当前的视图状态(Windows.UI.ViewManagement.ApplicationViewState 枚举)
  37. * Snapped - snap 后的小视图
  38. * Filled - snap 后的大视图
  39. * FullScreenLandscape - 全屏水平视图
  40. * FullScreenPortrait - 全屏垂直视图
  41. */
  42. ApplicationViewState currentState = ApplicationView.Value;
  43.  
  44. if (currentState == ApplicationViewState.Snapped)
  45. {
  46. lblMsg.Text += Environment.NewLine;
  47. lblMsg.Text += "This app is snapped";
  48. }
  49. else if (currentState == ApplicationViewState.Filled)
  50. {
  51. lblMsg.Text += Environment.NewLine;
  52. lblMsg.Text += "This app is in the fill state";
  53. }
  54. else if (currentState == ApplicationViewState.FullScreenLandscape)
  55. {
  56. lblMsg.Text += Environment.NewLine;
  57. lblMsg.Text += "This app is full-screen landscape";
  58. }
  59. else if (currentState == ApplicationViewState.FullScreenPortrait)
  60. {
  61. lblMsg.Text += Environment.NewLine;
  62. lblMsg.Text += "This app is full-screen portrait";
  63. }
  64. }
  65.  
  66. private void btnUnsnap_Click_1(object sender, RoutedEventArgs e)
  67. {
  68. /*
  69. * ApplicationView.TryUnsnap() - 尝试解除 snap,尝试之后返回当前是否是 unsnapped 状态
  70. */
  71. bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
  72.  
  73. lblMsg.Text += Environment.NewLine;
  74. lblMsg.Text += "unsnapped: " + unsnapped;
  75. }
  76. }
  77. }

4、演示如何为 ApplicationViewState 的变化增加动画效果
UI/ApplicationViewStateAnimation.xaml

  1. <Page
  2. x:Class="XamlDemo.UI.ApplicationViewStateAnimation"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <StackPanel Margin="120 0 0 0">
  13.  
  14. <TextBlock Name="lblMsg" FontSize="48" TextWrapping="Wrap" Text="为 snap 操作和屏幕方向的改变增加动画效果" Foreground="White" Margin="0 0 20 0" />
  15.  
  16. </StackPanel>
  17.  
  18. <!--
  19. 自定义 ApplicationViewState 变化时的动画效果,由 code-behind 中的 VisualStateManager 控制
  20. 关于 VisualState 请参见 XamlDemo/Controls/UI/VisualStateDemo.xaml
  21. -->
  22. <VisualStateManager.VisualStateGroups>
  23. <VisualStateGroup>
  24. <VisualState x:Name="Landscape">
  25. <Storyboard>
  26. <ColorAnimation Storyboard.TargetName="lblMsg" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
  27. To="White" />
  28. </Storyboard>
  29. </VisualState>
  30. <VisualState x:Name="Portrait">
  31. <Storyboard>
  32. <ColorAnimation Storyboard.TargetName="lblMsg" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
  33. To="Blue" />
  34. </Storyboard>
  35. </VisualState>
  36. <VisualState x:Name="Snapped">
  37. <Storyboard>
  38. <ColorAnimation Storyboard.TargetName="lblMsg" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
  39. To="Red" />
  40. </Storyboard>
  41. </VisualState>
  42. <VisualState x:Name="Filled">
  43. <Storyboard>
  44. <ColorAnimation Storyboard.TargetName="lblMsg" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
  45. To="Orange" />
  46. </Storyboard>
  47. </VisualState>
  48. <VisualStateGroup.Transitions>
  49. <VisualTransition To="Landscape" GeneratedDuration="0:0:1">
  50. <VisualTransition.GeneratedEasingFunction>
  51. <ElasticEase EasingMode="EaseInOut" />
  52. </VisualTransition.GeneratedEasingFunction>
  53. </VisualTransition>
  54. <VisualTransition To="Portrait" GeneratedDuration="0:0:1">
  55. <VisualTransition.GeneratedEasingFunction>
  56. <ElasticEase EasingMode="EaseInOut" />
  57. </VisualTransition.GeneratedEasingFunction>
  58. </VisualTransition>
  59. <VisualTransition To="Snapped" GeneratedDuration="0:0:1">
  60. <VisualTransition.GeneratedEasingFunction>
  61. <ElasticEase EasingMode="EaseInOut" />
  62. </VisualTransition.GeneratedEasingFunction>
  63. </VisualTransition>
  64. <VisualTransition To="Filled" GeneratedDuration="0:0:1">
  65. <VisualTransition.GeneratedEasingFunction>
  66. <ElasticEase EasingMode="EaseInOut" />
  67. </VisualTransition.GeneratedEasingFunction>
  68. </VisualTransition>
  69. </VisualStateGroup.Transitions>
  70. </VisualStateGroup>
  71. </VisualStateManager.VisualStateGroups>
  72.  
  73. </Grid>
  74. </Page>

UI/ApplicationViewStateAnimation.xaml.cs

  1. /*
  2. * 演示如何为 ApplicationViewState 的变化增加动画效果
  3. */
  4.  
  5. using Windows.UI.Core;
  6. using Windows.UI.ViewManagement;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9.  
  10. namespace XamlDemo.UI
  11. {
  12. public sealed partial class ApplicationViewStateAnimation : Page
  13. {
  14. public ApplicationViewStateAnimation()
  15. {
  16. this.InitializeComponent();
  17.  
  18. Window.Current.SizeChanged += Current_SizeChanged;
  19.  
  20. ChangeViewSate();
  21. }
  22.  
  23. void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
  24. {
  25. ChangeViewSate();
  26. }
  27.  
  28. void ChangeViewSate()
  29. {
  30. // 根据 ApplicationViewState 的变化触发相应的动画
  31. switch (ApplicationView.Value)
  32. {
  33. case ApplicationViewState.FullScreenLandscape:
  34. VisualStateManager.GoToState(this, "Landscape", true);
  35. break;
  36. case ApplicationViewState.FullScreenPortrait:
  37. VisualStateManager.GoToState(this, "Portrait", true);
  38. break;
  39. case ApplicationViewState.Snapped:
  40. VisualStateManager.GoToState(this, "Snapped", true);
  41. break;
  42. case ApplicationViewState.Filled:
  43. VisualStateManager.GoToState(this, "Filled", true);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. }
  50. }

5、演示 WinRT 中关于“缩放至不同屏幕”的概念
UI/Scale.xaml

  1. <Page
  2. x:Class="XamlDemo.UI.Scale"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="120 0 0 0" HorizontalAlignment="Left">
  12.  
  13. <TextBlock FontSize="14.667" LineHeight="20" TextWrapping="Wrap">
  14. <Run>1、区分屏幕的指标:尺寸,分辨率,密度</Run>
  15. <LineBreak />
  16. <Run>2、最小分辨率为 1024 * 768(无 snap),支持 snap 的最小分辨率为 1366 * 768</Run>
  17. <LineBreak />
  18. <Run>3、系统会根据屏幕密度自动进行缩放,也就是说在开发 app 的时候只要考虑分辨率的适应问题即可</Run>
  19. <LineBreak />
  20. <Run>4、系统有 3 种缩放倍数:100%, 140%, 180%</Run>
  21. <LineBreak />
  22. <Run>5、比如 10.6 寸屏幕:1366*768 会缩放至 100%,1920*1080 会缩放至 140%,2560*1440 会缩放至 180%</Run>
  23. <LineBreak />
  24. <Run>6、通过 Window.Current.Bounds 获取到的宽度和高度不是屏幕分辨率,而是屏幕分辨率与缩放相结合之后的值</Run>
  25. <LineBreak />
  26. <Run>7、px 是像素,dpi 是密度(每英寸的点数),pt 是 1/72 英寸,px = pt * dpi / 72,WinRT 中与尺寸相关的单位通常是 px</Run>
  27. <LineBreak />
  28. <Run>8、Package.appxmanifest 中引用的图片也支持 scale</Run>
  29. </TextBlock>
  30.  
  31. <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" />
  32.  
  33. <!--
  34. 在 /Assets/ 目录内放 3 个目录 scale-100, scale-140, scale-180, 每个目录内均放一个 MyImage.png 文件
  35. 它们分别对应 100% 缩放, 140% 缩放, 180% 缩放
  36. 通过 /Assets/MyImage.png 引用图片,系统会自动找到并使用对应缩放比的图片
  37. -->
  38. <Image Source="/Assets/MyImage.png" Width="100" Height="100" Margin="0 10 0 0" HorizontalAlignment="Left" VerticalAlignment="Top" />
  39.  
  40. <!--
  41. 在 /Assets/scale/ 目录内放 3 个文件 MyImage.scale-100.png, MyImage.scale-140.png, MyImage.scale-180.png
  42. 它们分别对应 100% 缩放, 140% 缩放, 180% 缩放
  43. 通过 /Assets/scale/MyImage.png 引用图片,系统会自动找到并使用对应缩放比的图片
  44. -->
  45. <Image Source="/Assets/scale/MyImage.png" Width="100" Height="100" Margin="0 10 0 0" HorizontalAlignment="Left" VerticalAlignment="Top" />
  46.  
  47. </StackPanel>
  48. </Grid>
  49. </Page>

UI/Scale.xaml.cs

  1. /*
  2. * 演示 WinRT 中关于“缩放至不同屏幕”的概念
  3. */
  4.  
  5. using System;
  6. using Windows.ApplicationModel.Resources.Core;
  7. using Windows.Graphics.Display;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10.  
  11. namespace XamlDemo.UI
  12. {
  13. public sealed partial class Scale : Page
  14. {
  15. public Scale()
  16. {
  17. this.InitializeComponent();
  18.  
  19. Window.Current.SizeChanged += Current_SizeChanged;
  20.  
  21. ShowInfo();
  22. }
  23.  
  24. void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
  25. {
  26. ShowInfo();
  27. }
  28.  
  29. private void ShowInfo()
  30. {
  31. // 通过 Window.Current.Bounds 获取到的宽度和高度不是屏幕分辨率,而是屏幕分辨率与缩放相结合之后的值
  32. lblMsg.Text = string.Format("Window.Current.Bounds: {0} * {1}", Window.Current.Bounds.Width, Window.Current.Bounds.Height);
  33. lblMsg.Text += Environment.NewLine;
  34.  
  35. // 获取屏幕的 dpi(dots per inch)
  36. lblMsg.Text += "LogicalDpi: " + DisplayProperties.LogicalDpi.ToString();
  37. lblMsg.Text += Environment.NewLine;
  38.  
  39. // 获取当前的缩放比例(Windows.Graphics.Display.ResolutionScale 枚举:Invalid, Scale100Percent, Scale140Percent, Scale180Percent)
  40. lblMsg.Text += "ResolutionScale: " + DisplayProperties.ResolutionScale.ToString();
  41. lblMsg.Text += Environment.NewLine;
  42.  
  43. // 另一个获取当前的缩放比例的方法
  44. string scale;
  45. ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Scale", out scale);
  46. lblMsg.Text += "Scale: " + scale;
  47. lblMsg.Text += Environment.NewLine;
  48. }
  49. }
  50. }

6、演示 WinRT 中关于“对比度”的概念
UI/HighContrast.xaml

  1. <Page
  2. x:Class="XamlDemo.UI.HighContrast"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="120 0 0 0">
  12.  
  13. <TextBlock Name="lblMsg" FontSize="14.667" />
  14.  
  15. <!--
  16. 注:Package.appxmanifest 中引用的图片也支持 high contrast
  17.  
  18. 有 3 种模式,分别是
  19. 1、标准模式
  20. 2、黑色主题的高对比度模式
  21. 3、白色主题的高对比度模式
  22.  
  23. 系统资源包含了对全部 3 种模式的支持
  24. -->
  25. <Button Content="默认的 Button 样式,启用高对比度模式(电脑设置 -> 轻松使用 -> 高对比度)可查看效果" Margin="0 10 0 0" />
  26.  
  27. <!--
  28. 具体图片文件请查看 /Assets/highContrast/ 目录
  29. 1、xxx.contrast-standard.png 对应 标准模式
  30. 2、xxx.contrast-black.png 对应 黑色主题的高对比度模式
  31. 3、xxx.contrast-white.png 对应 白色主题的高对比度模式
  32. 4、xxx.scale-100_contrast-standard.png 或 xxx.contrast-standard_scale-100.png 对应 scale 与 HighContrast 相结合的方式
  33. -->
  34. <Image Source="/Assets/highContrast/TheImage.png" Width="200" Height="100" Margin="0 10 0 0" HorizontalAlignment="Left" />
  35.  
  36. <!--
  37. 通过 ResourceDictionary.ThemeDictionaries 来指定 3 种模式下的不同资源
  38. Default 代表 标准模式
  39. HighContrastBlack 代表 黑色主题的高对比度模式
  40. HighContrastWhite 代表 白色主题的高对比度模式
  41. -->
  42. <StackPanel Margin="0 10 0 0">
  43. <StackPanel.Resources>
  44. <ResourceDictionary>
  45. <ResourceDictionary.ThemeDictionaries>
  46. <ResourceDictionary x:Key="Default">
  47. <SolidColorBrush x:Key="TargetForeground" Color="Red"/>
  48. </ResourceDictionary>
  49. <ResourceDictionary x:Key="HighContrastBlack">
  50. <SolidColorBrush x:Key="TargetForeground" Color="White"/>
  51. </ResourceDictionary>
  52. <ResourceDictionary x:Key="HighContrastWhite">
  53. <SolidColorBrush x:Key="TargetForeground" Color="Black"/>
  54. </ResourceDictionary>
  55. </ResourceDictionary.ThemeDictionaries>
  56. </ResourceDictionary>
  57. </StackPanel.Resources>
  58.  
  59. <TextBlock Text="启用高对比度模式可查看效果" FontSize="14.667" Foreground="{StaticResource TargetForeground}" />
  60. </StackPanel>
  61.  
  62. </StackPanel>
  63. </Grid>
  64. </Page>

UI/HighContrast.xaml.cs

  1. /*
  2. * 演示 WinRT 中关于“对比度”的概念
  3. */
  4.  
  5. using Windows.ApplicationModel.Resources.Core;
  6. using Windows.UI.Xaml.Controls;
  7. using Windows.UI.Xaml.Navigation;
  8.  
  9. namespace XamlDemo.UI
  10. {
  11. public sealed partial class HighContrast : Page
  12. {
  13. public HighContrast()
  14. {
  15. this.InitializeComponent();
  16. }
  17.  
  18. protected override void OnNavigatedTo(NavigationEventArgs e)
  19. {
  20. string contrast;
  21. // 获取当前的对比度模式
  22. ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Contrast", out contrast);
  23. lblMsg.Text = "current contrast: " + contrast;
  24. }
  25. }
  26. }

OK
[源码下载]

重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInformation

    [源码下载] 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInfor ...

  3. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  4. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  5. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  6. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  7. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  8. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  9. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

随机推荐

  1. How to create an anonymous IDA PRO database (.IDB)

    Source: http://www.0xebfe.net/blog/2013/01/13/how-to-create-an-anonymous-ida-pro-database-dot-idb/ P ...

  2. Spring3系列4-多个配置文件的整合

    Spring3系列4-多个配置文件的整合 在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将 ...

  3. UNIX环境高级编程笔记之进程环境

    本章讲的都是一些非常基础的知识,目的是为了下一章讲进程控制做铺垫,所以,本章就不做过多的总结了,直接看图吧.

  4. dlib库使用

    最近的工作中用到了dlib这个库,该库是一个机器学习的开源库,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).不过由于是开源的,所以bug多少有一些,我在example ...

  5. [LeetCode] 桶排序的特殊解,例 Sort Color

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  6. Python Django 开发 2 数据库

    一半教程用的Django都是1.8以前的版本,导致跟我用的1.8.2的版本用法有些出入,所以只能自己去官网看文档,以下一下是看官方文档而理解的,英语渣渣,可能会有理解有误的地方 先记录下如何查看dja ...

  7. NVARCHAR 和VARCHAR区别和使用

    1.各自的定义: ► nvarchar(n) : 包含   n   个字符的可变长度   Unicode   字符数据.n   的值必须介于   1   与   4,000   之间.字节的存储大小是 ...

  8. ES6 Promise 接口

    构造函数 new Promise(function(resolve, reject){}); 构造函数接受一个函数(executor)作为参数,该函数在返回 Promise 实例之前被调用.函数的两个 ...

  9. 匹配img和a

    a:<\s*a\shref=*([^>]*)>([^<]|<(?!/a))*<\s*/a\s*> img:<img\b[^<>]*?\bsr ...

  10. Android 布局之GridLayout

    Android 布局之GridLayout 1 GridLayout简介 GridLayout是Android4.0新提供的网格矩阵形式的布局控件. GridLayout的继承关系如下:java.la ...