设为首页 收藏本站
查看: 793|回复: 0

重新想象 Windows 8 Store Apps (56)

[复制链接]

尚未签到

发表于 2015-5-22 13:20:11 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 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



/*
* 演示如何获取系统的 UI 相关的设置信息
*/
using System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.UI
{
public sealed partial class UISettingsInfo : Page
{
public UISettingsInfo()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
UISettings uiSettings = new UISettings();
lblMsg.Text = "AnimationsEnabled: " + uiSettings.AnimationsEnabled; // 是否启用动画
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CaretBlinkRate: " + uiSettings.CaretBlinkRate; // 输入光标的闪烁速率
lblMsg.Text += Environment.NewLine;
/*
* 光标浏览模式(Caret Browsing) - 在页面中会出现一个类似于记事本中的输入光标,用户可以使用键盘(按 Shift 键 + 方向键)来精确地进行页面文字的选择
* IE8 以上可以通过“F7”来打开/关闭光标浏览模式
*/
lblMsg.Text += "CaretBrowsingEnabled: " + uiSettings.CaretBrowsingEnabled; // 当前输入光标是否可用于光标浏览模式
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CaretWidth: " + uiSettings.CaretWidth; // 输入光标的宽度
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CursorSize: " + uiSettings.CursorSize; // 指针的尺寸
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DoubleClickTime: " + uiSettings.DoubleClickTime; // 捕获双击时,两次单击间的最长时间
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "HandPreference: " + uiSettings.HandPreference; // 用户界面的方向(LeftHanded 或 RightHanded)
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "MessageDuration: " + uiSettings.MessageDuration; // 消息显示的持续时间,单位:秒
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "MouseHoverTime: " + uiSettings.MouseHoverTime; // hover 事件触发之前,指针可以 hover 的时间
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ScrollBarArrowSize: " + uiSettings.ScrollBarArrowSize; // 当前窗口滚动条的箭头的大小
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ScrollBarSize: " + uiSettings.ScrollBarSize; // 当前窗口滚动条的大小
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ScrollBarThumbBoxSize: " + uiSettings.ScrollBarThumbBoxSize; // 当前窗口滚动条 thumb 的大小
lblMsg.Text += Environment.NewLine;

// 获取当前系统的相关颜色
lblMsg.Text += "ActiveCaption: " + uiSettings.UIElementColor(UIElementType.ActiveCaption);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Background: " + uiSettings.UIElementColor(UIElementType.Background);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ButtonFace: " + uiSettings.UIElementColor(UIElementType.ButtonFace);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ButtonText: " + uiSettings.UIElementColor(UIElementType.ButtonText);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CaptionText: " + uiSettings.UIElementColor(UIElementType.CaptionText);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "GrayText: " + uiSettings.UIElementColor(UIElementType.GrayText);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Highlight: " + uiSettings.UIElementColor(UIElementType.Highlight);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "HighlightText: " + uiSettings.UIElementColor(UIElementType.HighlightText);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Hotlight: " + uiSettings.UIElementColor(UIElementType.Hotlight);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "InactiveCaption: " + uiSettings.UIElementColor(UIElementType.InactiveCaption);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "InactiveCaptionText: " + uiSettings.UIElementColor(UIElementType.InactiveCaptionText);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Window: " + uiSettings.UIElementColor(UIElementType.Window);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "WindowText: " + uiSettings.UIElementColor(UIElementType.WindowText);
lblMsg.Text += Environment.NewLine;

AccessibilitySettings accessibilitySettings = new AccessibilitySettings();
lblMsg.Text += "是否启用了高对比度模式: " + accessibilitySettings.HighContrast; // 是否启用了高对比度模式
        }
}
}
  
2、演示与“屏幕方向”相关的知识点
UI/ScreenOrientation.xaml











  UI/ScreenOrientation.xaml.cs



/*
* 演示与“屏幕方向”相关的知识点
*/
using System;
using Windows.Graphics.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.UI
{
public sealed partial class ScreenOrientation : Page
{
public ScreenOrientation()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 平板上的“windows”键相对于平板本身的方向
lblMsg.Text = "NativeOrientation: " + DisplayProperties.NativeOrientation.ToString();
lblMsg.Text += Environment.NewLine;
// 平板的方向(Windows.Graphics.Display.DisplayOrientations 枚举:None, Landscape, Portrait, LandscapeFlipped, PortraitFlipped)
// 注:Landscape 顺时针转是 Portrait
lblMsg.Text += "CurrentOrientation: " + DisplayProperties.CurrentOrientation.ToString();
// DisplayProperties.CurrentOrientation 发生变化时触发的事件
DisplayProperties.OrientationChanged += DisplayProperties_OrientationChanged;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DisplayProperties.OrientationChanged -= DisplayProperties_OrientationChanged;
}
void DisplayProperties_OrientationChanged(object sender)
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CurrentOrientation: " + DisplayProperties.CurrentOrientation.ToString();
}
private void btnLock_Checked_1(object sender, RoutedEventArgs e)
{
// DisplayProperties.AutoRotationPreferences - 指定当前 app 所支持的方向,即仅允许设备支持指定的方向(模拟器中无效)
// 注:可在 Package.appxmanifest 中指定
DisplayProperties.AutoRotationPreferences = DisplayProperties.CurrentOrientation;
btnLock.Content = "解除方向锁定";
}
private void btnLock_Unchecked_1(object sender, RoutedEventArgs e)
{
DisplayProperties.AutoRotationPreferences = DisplayOrientations.None;
btnLock.Content = "锁定当前方向";
}
}
}
  
3、演示与“snap”相关的知识点
UI/Snap.xaml








snap 可以通过手势操作,也可以通过快捷键:win + .




  UI/Snap.xaml.cs



/*
* 演示与“snap”相关的知识点
*
* 注:
* snap 视图的宽度是固定的:320 像素
* 支持 snap 的最小分辨率为 1366 * 768
*/
using System;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.UI
{
public sealed partial class Snap : Page
{
public Snap()
{
this.InitializeComponent();
Window.Current.SizeChanged += Current_SizeChanged;
ShowCurrentApplicationViewState();
}
void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
ShowCurrentApplicationViewState();
}
void ShowCurrentApplicationViewState()
{
/*
* ApplicationView.Value - 获取当前的视图状态(Windows.UI.ViewManagement.ApplicationViewState 枚举)
*     Snapped - snap 后的小视图
*     Filled - snap 后的大视图
*     FullScreenLandscape - 全屏水平视图
*     FullScreenPortrait - 全屏垂直视图
*/
ApplicationViewState currentState = ApplicationView.Value;
if (currentState == ApplicationViewState.Snapped)
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "This app is snapped";
}
else if (currentState == ApplicationViewState.Filled)
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "This app is in the fill state";
}
else if (currentState == ApplicationViewState.FullScreenLandscape)
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "This app is full-screen landscape";
}
else if (currentState == ApplicationViewState.FullScreenPortrait)
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "This app is full-screen portrait";
}
}
private void btnUnsnap_Click_1(object sender, RoutedEventArgs e)
{
/*
* ApplicationView.TryUnsnap() - 尝试解除 snap,尝试之后返回当前是否是 unsnapped 状态
*/
bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "unsnapped: " + unsnapped;
}
}
}
  
4、演示如何为 ApplicationViewState 的变化增加动画效果
UI/ApplicationViewStateAnimation.xaml

























































  UI/ApplicationViewStateAnimation.xaml.cs



/*
*  演示如何为 ApplicationViewState 的变化增加动画效果
*/
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.UI
{
public sealed partial class ApplicationViewStateAnimation : Page
{
public ApplicationViewStateAnimation()
{
this.InitializeComponent();
Window.Current.SizeChanged += Current_SizeChanged;
ChangeViewSate();
}
void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
ChangeViewSate();
}
void ChangeViewSate()
{
// 根据 ApplicationViewState 的变化触发相应的动画
switch (ApplicationView.Value)
{
case ApplicationViewState.FullScreenLandscape:
VisualStateManager.GoToState(this, "Landscape", true);
break;
case ApplicationViewState.FullScreenPortrait:
VisualStateManager.GoToState(this, "Portrait", true);
break;
case ApplicationViewState.Snapped:
VisualStateManager.GoToState(this, "Snapped", true);
break;
case ApplicationViewState.Filled:
VisualStateManager.GoToState(this, "Filled", true);
break;
default:
break;
}
}
}
}
  
5、演示 WinRT 中关于“缩放至不同屏幕”的概念
UI/Scale.xaml







1、区分屏幕的指标:尺寸,分辨率,密度

2、最小分辨率为 1024 * 768(无 snap),支持 snap 的最小分辨率为 1366 * 768

3、系统会根据屏幕密度自动进行缩放,也就是说在开发 app 的时候只要考虑分辨率的适应问题即可

4、系统有 3 种缩放倍数:100%, 140%, 180%

5、比如 10.6 寸屏幕:1366*768 会缩放至 100%,1920*1080 会缩放至 140%,2560*1440 会缩放至 180%

6、通过 Window.Current.Bounds 获取到的宽度和高度不是屏幕分辨率,而是屏幕分辨率与缩放相结合之后的值

7、px 是像素,dpi 是密度(每英寸的点数),pt 是 1/72 英寸,px = pt * dpi / 72,WinRT 中与尺寸相关的单位通常是 px

8、Package.appxmanifest 中引用的图片也支持 scale









  UI/Scale.xaml.cs



/*
* 演示 WinRT 中关于“缩放至不同屏幕”的概念
*/
using System;
using Windows.ApplicationModel.Resources.Core;
using Windows.Graphics.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.UI
{
public sealed partial class Scale : Page
{
public Scale()
{
this.InitializeComponent();
Window.Current.SizeChanged += Current_SizeChanged;
ShowInfo();
}
void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
ShowInfo();
}
private void ShowInfo()
{
// 通过 Window.Current.Bounds 获取到的宽度和高度不是屏幕分辨率,而是屏幕分辨率与缩放相结合之后的值
lblMsg.Text = string.Format("Window.Current.Bounds: {0} * {1}", Window.Current.Bounds.Width, Window.Current.Bounds.Height);
lblMsg.Text += Environment.NewLine;
// 获取屏幕的 dpi(dots per inch)
lblMsg.Text += "LogicalDpi: " + DisplayProperties.LogicalDpi.ToString();
lblMsg.Text += Environment.NewLine;
// 获取当前的缩放比例(Windows.Graphics.Display.ResolutionScale 枚举:Invalid, Scale100Percent, Scale140Percent, Scale180Percent)
lblMsg.Text += "ResolutionScale: " + DisplayProperties.ResolutionScale.ToString();
lblMsg.Text += Environment.NewLine;
// 另一个获取当前的缩放比例的方法
string scale;
ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Scale", out scale);
lblMsg.Text += "Scale: " + scale;
lblMsg.Text += Environment.NewLine;
}
}
}
  
6、演示 WinRT 中关于“对比度”的概念
UI/HighContrast.xaml

































  UI/HighContrast.xaml.cs



/*
* 演示 WinRT 中关于“对比度”的概念
*/
using Windows.ApplicationModel.Resources.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.UI
{
public sealed partial class HighContrast : Page
{
public HighContrast()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string contrast;
// 获取当前的对比度模式
ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Contrast", out contrast);
lblMsg.Text = "current contrast: " + contrast;
}
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69602-1-1.html 上篇帖子: Windows 8实用窍门系列:22.Windows 8 的SemanticZoom缩放视图 下篇帖子: 快速构建Windows 8风格应用2-创建调试应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表