Window Phone 编程
1.磁砖简介
Window Phone 8(以下简称WP8)中包含3种不同大小的磁砖(Wide(大磁砖),Medium(中磁砖),Small(小磁砖)),而WP8目前支持2中类型分辨率WXGA和720P,数据如下表:
单位(像素) |
File and Cycle |
Iconic |
Wide |
691*336 |
N/A |
Medium |
336*336 |
202*202 |
Small |
159*159 |
101*101 |
新的磁砖由数据模板提供数据的支持,数据模板由以下三个类:FlieTileData、CycleTileData和IconicTileData。
FlieTileData(翻转磁砖数据模板):StrandTileData
描述一个从前端到后端的平铺数据模板,允许指定的背景图像和文本。
该类对应的属性值,与下图对比(图1.1):
图1.1
以及磁砖的像素大小解释如图(1.2):
图1.2
如何创建一个FlieTileData,分为2中方式,一种是通过XAML,一种是通过后台.cs代码编程。
XAML方式:
后台编程方式:
适用场景:
1. 例如:一个天气应用程序,需要在前面显示当前的温度,在后面显示将来5天的天气情况。
2. 主要用于如何分开显示当前和后台数据。
l CycleTileData(周期磁砖数据模板):ShellTileData
可以实现从1-9的背景图片按照周期性的改变的磁砖。
类的属性值对应图:
图1.3
磁砖大小像素如图:
图1.4
XMAL实现方式:
后台代码编程实现方式:
适用场景:
1. 照片应用程序,或者是社交照片流程序
2. 当前正在播放的专辑封面艺术家,歌曲在播放列表中
l IconicTileData(标志性的磁砖数据模板):ShellTileData
描述一个有标志性的磁砖数据模板
类的属性值对应的图:
图1.5
磁砖像素大小如图:
图1.6
XMAL实现方式:
后台编程代码:
适用场景:
消息,语音邮件,新闻阅读器,和一般的通信应用程序,只要是非常重要消息
社交类的应用程序,需要实现在线消息,实时消息的数据显示
在WP8手机锁屏的中,所有应用程序将不可用,只有解锁才能适用,但是在锁屏中也是有一块通知区域可以显示一些应用程序(在WP7.1中不允许第三方应用程序加入锁屏通知区域,但是在WP8中允许第三方应用程序,而且支持用户自定义通知区域程序显示),如图:
WP8系统提供自定义设置你的应用程序成为锁屏区域显示程序,如图:
如何实现将你的应用程序支持设置为锁屏区域的应用程序,你只需执行以下步骤:
创建一个锁屏小图标
创建一个38*38像素的PNG小图标文件,图标中必须包含白色像素以及一定的透明度。
选择你的解决方案,找到属性节点(Properties),然后右击WMAppManifest.xml文件以代码的形式打开该文件。
找到DeviceLockImageURI节点,将小图标的全路径写入节点内容,然后如果是相对路径则设置IsRelative=true,如果是来自资源文件,则将IsResource=true,反之亦然。
如:<DeviceLockImageURI IsRelative=”true” IsResource=”False”>
Assets\LockImage.png
</DeviceLockImageURI>
2. 设置应用程序的配置文件:ManiFest.xml的扩展
同上面一样,打开WMAppManifest.xml文件,然后找到Extensions节点,如果不存在该节点,则在Tokens节点下手动添加该节点。如果你想让通知区域显示磁砖编号,用LockScreen_Notification_IconCount,如果想让显示文本,则用LockScreen_Notification_TextField。
<Extensions>
<Extension ExtensionName=” LockScreen_Notification_IconCount
”ConsumeID=”{111DFF24-AA15-4A96-8006-2BFF8122084F}” TaskID=”_Default”/>
<Extension ExtensionName=” LockScreen_Notification_TextField
”ConsumeID=”{111DFF24-AA15-4A96-8006-2BFF8122084F}” TaskID=”_Default”/></Extensions>
3. 为锁屏区域通知界面设置一个链接
private async void btnGoToLockSettings_Click(object sender, RoutedEventArgs e)
{
// Launch URI for the lock screen settings screen.
var op = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:"));
}
如何实现锁屏区域的背景图片更换效果
锁屏区域背景图片区域指定的锁屏整个手机背景区域,如图:
实现步骤如下:
设置应用程序的配置文件:ManiFest.xml的扩展
同上面一样,打开WMAppManifest.xml文件,然后找到Extensions节点,如果不存在该节点,则在Tokens节点下手动添加该节点。如果想让背景图片更改,用LockScreen _BackGround,
<Extensions>
<Extension ExtensionName=” LockScreen _BackGround
”ConsumeID=”{111DFF24-AA15-4A96-8006-2BFF8122084F}” TaskID=”_Default”/>
后台添加.cs代码用以改变背景图片
private async void LockHelper(string filePathOfTheImage, bool isAppResource)
{
try
{
var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
if (!isProvider)
{
// If you're not the provider, this call will prompt the user for permission.
// Calling RequestAccessAsync from a background agent is not allowed.
var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();
// Only do further work if the access was granted.
isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
}
if (isProvider)
{
// At this stage, the app is the active lock screen background provider.
// The following code example shows the new URI schema.
// ms-appdata points to the root of the local app data folder.
// ms-appx points to the Local app install folder, to reference resources bundled in the XAP package.
var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);
// Set the lock screen background image.
Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);
// Get the URI of the lock screen background image.
var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri();
System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());
}
else
{
MessageBox.Show("You said no, so I can't update your background.");
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
c) 如果是采用是隔离存储,则需要给图片唯一命名
string fileName;
var currentImage = LockScreen.GetImageUri();
if (currentImage.ToString().EndsWith("_A.jpg"))
{
fileName = "LiveLockBackground_B.jpg";
}
else
{
fileName = "LiveLockBackground_A.jpg";
}
var lockImage = string.Format("{0}", fileName);
// At this point in the code, write the image to isolated storage.
必须给你的应用程序指定默认的背景图片,这样在锁屏的时候才能显示默认的背景图片。
在锁屏界面点击可以调用OnNavigatedTo事件获取相应的参数:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string lockscreenKey = "WallpaperSettings";
string lockscreenValue = "0";
bool lockscreenValueExists = NavigationContext.QueryString.TryGetValue(lockscreenKey, out lockscreenValue);
if (lockscreenValueExists)
{
// Navigate the user to your app's lock screen settings screen here,
// or indicate that the lock screen background image is updating.
}
}
使你的应用程序能自动链接到系统应用程序设置界面。
private async void btnGoToLockSettings_Click(object sender, RoutedEventArgs e)
{
// Launch URI for the lock screen settings screen.
var op = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:"));
}
WP8应用程序支持三种分辨率模式:WVGA、WXGA、720P。具体见文章开头已经详细介绍了。如图:
合理处理你的应用程序布局。
在WP7.1中只有一种分比率模式,所以使用不需要考虑分比率带来的问题。但是WP8中是支持3中分辨率,以及15:9和16:9的比例。所以在处理WP8布局中不要硬编码每一个布局的高和宽,而应该尽量采用*(占用比例)和Auto值去设置布局宽和高。
如何处理不同分辨率的背景图片?
准备3张不同分辨率的背景图片,分别命名:MyImage.screen-wvga.png,MyImage.screen-wxga.png,, MyImage.screen-720p.png
创建一个ResolutionHelper.cs类,进行对图片判断。
public enum Resolutions { WVGA, WXGA, HD720p };
public static class ResolutionHelper
{
private static bool IsWvga
{
get
{
return App.Current.Host.Content.ScaleFactor == 100;
}
}
private static bool IsWxga
{
get
{
return App.Current.Host.Content.ScaleFactor == 160;
}
}
private static bool Is720p
{
get
{
return App.Current.Host.Content.ScaleFactor == 150;
}
}
public static Resolutions CurrentResolution
{
get
{
if (IsWvga) return Resolutions.WVGA;
else if (IsWxga) return Resolutions.WXGA;
else if (Is720p) return Resolutions.HD720p;
else throw new InvalidOperationException("Unknown resolution");
}
}
}
在创建MultiResImageChooser.cs类进行选择
using System.Windows.Media.Imaging;
public class MultiResImageChooserUri
{
public Uri BestResolutionImage
{
get
{
switch (ResolutionHelper.CurrentResolution)
{
case Resolutions.HD720p:
return new Uri("Assets/MyImage.screen-720p.jpg", UriKind.Relative);
case Resolutions.WXGA:
return new Uri("Assets/MyImage.screen-wxga.jpg", UriKind.Relative);
case Resolutions.WVGA:
return new Uri("Assets/MyImage.screen-wvga.jpg", UriKind.Relative);
default:
throw new InvalidOperationException("Unknown resolution type");
}
}
}
}
在MainPage.xaml,主界面中XAML中进行数据绑定
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Source="{Binding BestResolutionImage, Source={StaticResource MultiResImageChooser}}"/>
</Grid>
最后在应用程序资源文件中加入资源引用
xmlns:h="clr-namespace:MultiResSnippet"
<!--Application Resources-->
<Application.Resources>
<h:MultiResImageChooser x:Key="MultiResImageChooser"/>
</Application.Resources>
WP8支持2中应用程序模型,一种是XAML,一种是Direct3D。XAML是支持C#和VB语言,一般用于应用程序开发,而Dircet3D则是由C++开发人员开发的3D视图,主要应用于游戏开发。
在WP8中可以采用Microsoft.Xna.Framework.Media.PhoneExtensions.dll组件中的MediaLibrary类的SaveSong()和Delete()等方法实现对音频多媒体的保存和删除等功能。
在WP8中Microsoft.Xna.Framework.MediaLibraryExtensions.dll.组件中可以进行对图片的相关操作方法。如:
1.使用Picture 类中的Getthumbnail()获取图片的缩略图。
2.使用Picture 类中的 GetPreviewImage()获取图片的预览效果。
3.使用Picture 类中的获取图片路径,获取图片文件信息以及共享图片等功能。
可以通过PlayStateChangeEvents事件获取BackGroundAudioPlayer对象,以下是后台播放状态信息:
· IntermediatePlayState = BufferingStopped
· CurrentPlayState = Playing
· IntermediatePlayState = TrackEnded
· CurrentPlayState = Stopped