Windows统一平台: 开发小技巧
Windows统一平台: 开发小技巧
技巧一: 在手机端拓展你应用的显示区域。(WP8.1中也适用) 对于Windows Phone系统的手机, 手机屏幕最上方为系统状态栏(System Tray),在手机屏幕下方为应用指令栏(BottomAppbar), 我们写的应用的显示区域位于在这两者之间。 若要使应用的显示区域拓宽, 使用到了Windows.UI.ViewManagement这个类库, 借助于其中的方法, 可以实现应用的全屏显示,即充满整个手机屏幕。
示例代码:
var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
appView.SetDesiredBoundsMode(ApplicationViewBoundsMode.UseCoreWindow);
技巧二: 检测应用当前运行的设备类型 假设程序员要在代码中实现自适应的应用界面, 例如, 对于桌面系统, 使用MasterDetails控件, 对于移动系统, 使用ListView来显示数据。 检测当前设备类型的程序逻辑使用到了Windows.System.Profile.AnalyticsInfo这个类库。
示例代码:
var platformFamily = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily;
platformFamily可选的值为Windows.Desktop, Windows.Mobile, etc。
技巧三: 页面的导航 UWP应用中页面的导航相较于WP8.1发生了变化, UWP中没有提供NavigationManager这样的类来实现页面导航, 主要是因为UWP以后, 同一套代码既运行在移动端, 也运行在桌面端。当然, 你可以去扩展Phone extension来像8.1上一样使用NavigationManager, 但是更简单的办法是使用Windows.UI.Core.SystemNavigationManager这个类库, 使用这个方法能够兼顾到桌面端和移动端。 首先, 为了能使整个导航机制在所有页面上执行, 我们需要修改App.xaml.cs文件, 在OnLaunched(LaunchActivatedEventArgs e)方法中加入几行代码。
示例代码:
rootFrame.Navigated += (s, arg) =>
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
((Frame)s).CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;
};
SystemNavigationManager.GetForCurrentView().BackRequested += (ss, arg1) =>
{
Frame rFrame = Window.Current.Content as Frame;
if (rFrame.CanGoBack)
{
arg1.Handled = true;
rFrame.GoBack();
}
};
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
rootFrame.CanGoBack ?
AppViewBackButtonVisibility.Visible :
AppViewBackButtonVisibility.Collapsed;
技巧四:关于Adaptive UI
使用VisualStateManager来管理UI控件的布局,触发器是
MinWindowWidth、MinWindowHeight,当窗口大小发生变化时,控件的布局随着发生相应的变化。 刚开始实验,我使用了Grid和StackPanel, 使用StateManger控制StackPanel的方向,代码类似于:
<Page
x:Class="Dev209_T10.Views.DemoUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Dev209_T10.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> <StackPanel Orientation="Vertical" Grid.Column="0" Grid.Row="0">
<StackPanel x:Name="myPanel1" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel2" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel3" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel4" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
</StackPanel>
</Grid> <VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="wideState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="700" />
</VisualState.StateTriggers>
</VisualState>
<VisualState x:Name="narrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="myPanel1.Orientation" Value="Vertical" />
<Setter Target="myPanel2.Orientation" Value="Vertical" />
<Setter Target="myPanel3.Orientation" Value="Vertical" />
<Setter Target="myPanel4.Orientation" Value="Vertical" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Page>
运行时,不起任何作用, 然后通过实验多次,发现需要把VisualStateManger放到Grid里边,就起作用了。
<Page
x:Class="Dev209_T10.Views.DemoUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Dev209_T10.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> <StackPanel Orientation="Vertical" Grid.Column="0" Grid.Row="0">
<StackPanel x:Name="myPanel1" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel2" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel3" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
<StackPanel x:Name="myPanel4" Orientation="Horizontal" HorizontalAlignment="Left">
<Border Background="White" Height="20" Width="100" Margin="4"/>
<Border Background="Beige" Height="20" Width="200" Margin="4"/>
</StackPanel>
</StackPanel> <VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="wideState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="700" />
</VisualState.StateTriggers>
</VisualState>
<VisualState x:Name="narrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="myPanel1.Orientation" Value="Vertical" />
<Setter Target="myPanel2.Orientation" Value="Vertical" />
<Setter Target="myPanel3.Orientation" Value="Vertical" />
<Setter Target="myPanel4.Orientation" Value="Vertical" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</Page>
同样的,如果不想在XAML中设置StateTrigger, 也可以在代码中进行控制:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
this.SizeChanged += (o, s) =>
{
var state = "VisualStatePC";
if (s.NewSize.Width > )
state = "VisualStatePC";
else
state = "VisualStatePhone"; bool result = VisualStateManager.GoToState(this, state, true); };
}
技巧五: 使用Moblie Extension SDK
UWP App除了可以调用所有设备家族通用的WinRT API之外,还以调用针对特点设备的API,在使用之前只要引入相应的类库。 引用类库的步骤: 在项目中,单击References,选择Add References, 选择Universal Windows, 选择Extensions. 这里我们选中 Windows Mobile Extensions for the UWP,单击OK. 以拍照来说,在使用之前,要先来判断是不是Windows Phone, 对Windows Phone设备,当按下拍照按键时,触发相应程序逻辑。
示例代码:
using Windows.Storage.Pickers; bool isPhoneAvailable =
ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");
if (isPhoneAvailable)
{
Windows.Phone.UI.Input.HardwareButtons.CameraPressed += HardwareButtons_CameraPressed;
} private async void HardwareButtons_CameraPressed(object sender, Windows.Phone.UI.Input.CameraEventArgs e)
{
var cameraUI = new CameraCaptureUI();
cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; StorageFile photo = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo != null)
{
using (IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
imageControl.Source = bitmapSource;
}
}
}
Windows统一平台: 开发小技巧的更多相关文章
- Java开发小技巧(三):Maven多工程依赖项目
前言 本篇文章基于Java开发小技巧(二):自定义Maven依赖中创建的父工程project-monitor实现,运用我们自定义的依赖包进行多工程依赖项目的开发. 下面以多可执行Jar包项目的开发为例 ...
- BizTalk开发小技巧
BizTalk开发小技巧 随笔分类 - Biztalk Biztalk 使用BizTalk实现RosettaNet B2B So Easy 摘要: 使用BizTalk实现RosettaNet B2B ...
- flex开发小技巧集锦
关于flex开发网上有非常多的相关信息介绍,因此我们要想学习关于flex开发的知识信息技能是一件非常简单和方便的事情.而针对于flex开发小编要告诉大家的是一些flex开发小技巧.利用这些小技巧能够有 ...
- TP开发小技巧
TP开发小技巧原文地址http://wp.chenyuanzhao.com/wp/2016/07/23/tp%E5%BC%80%E5%8F%91%E5%B0%8F%E6%8A%80%E5%B7%A7/ ...
- 移动Web开发小技巧
移动Web开发小技巧 添加到主屏后的标题(IOS) name="apple-mobile-web-app-title" content="标题"> 启用 ...
- iOS开发小技巧 - UILabel添加中划线
iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...
- PHP开发小技巧②—实现二维数组根据key进行排序
在PHP中内置了很多对数组进行处理的函数,有很多时候我们直接使用其内置函数就能达到我们的需求,得到我们所想要的结果:但是,有的时候我们却不能通过使用内置函数实现我们的要求,这就需要我们自己去编写算法来 ...
- PHP开发小技巧③—实现多维数组转化为一维数组
在平常的项目开发中我们多会用到让多维数组转化为一维数组的情况,但是很多Programmer不会将其进行转化,也有些没有想到很好的算法然后经过乱起八糟的运算方式将其勉强转化好,但是所写的程序代码冗余非常 ...
- PHP开发小技巧①①—php实现手机号码显示部分
从个人信息保护性的角度来讲,我们在开发过程中总会想办法去保护用户的一些个人信息.就如本篇博文所讲,我们有时会将用户的手机号码只显示出部分,这是很多网站都有做的功能.这个功能实现起来也是特别的简单,只需 ...
随机推荐
- 各种浏览器hack
Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...
- [转]Windows的窗口刷新机制
1.Windows的窗口刷新管理 窗口句柄(HWND)都是由操作系统内核管理的,系统内部有一个z-order序列,记录着当前窗口从屏幕底部(假象的从屏幕到眼睛的方向),到屏幕最高层的一个窗口句柄的排序 ...
- linux命令行快捷键
linux命令行编辑快捷键 先总结几个个人觉得最有用的 ctrl + ? 撤消前一次输入 ctrl + c 另起一行 ctrl + r 输入单词搜索历史命令 ctrl + u 删除光标前面所有字符相当 ...
- 修改weblogic PermGen
vim /weblogic/Oracle/Middleware/wlserver_10.3/common/bin/commEnv.sh 在第144行,增加环境变量:JAVA_VENDOR=Sun #根 ...
- 本地调试WordPress计划终告失败
小猪本来想把博客的网站数据迁移到自己的电脑上面,mysql数据库还是放在主机供应商,这样就能缓解一下每次写博客时访问速度捉急的状况. 计划是美满的,但是只到实施的时候才发现各种问题.先是直接运行程序时 ...
- office 使用技巧
Excel: 非打印区域显示成灰色:视图--分页预览 选定行的时侯,如何从某一行选定到末尾? 先点某一行,然后按住shift不松,再按END,再按下方向箭. 选定列也是这样,先按住某列,然后按SHIF ...
- Jmeter java.lang.OutOfMemoryError: GC overhead limit exceeded
使用这个jmeter工具测试时,遇到这么个gc错误,网上找到了解决方案.原因是jmeter默认分配内存的参数很小,好像是256M吧.故而解决方法,就是增加内存: set HEAP=-Xms4g -Xm ...
- 常州培训 day1 解题报告
第一题:(骗分容易,AC难.) 题目大意: 给出一个字符串,找出满足条件A的区间的个数.A:字符A,B,C的出现次数相同. 都出现0次也算,区间的长度可以是0(就是只有一个数).30% |S| ≤ 1 ...
- 纯JS省市区三级联动
代码下载
- C 记录
为什么调用 sqrt 函数报错显示未定义 一.调用此函数时,要先引用头文件:#include <math.h>二.linux gcc 编译时,如果用到了 math中的函数,要手工加入函数库 ...