您的位置:首页 > 移动开发

重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期

2014-03-09 17:36 1021 查看
原文:重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期[源码下载]

[align=center]重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期[/align]

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 其它

文件压缩和解压缩

与 Windows 商店相关的操作

app 与 web

几个 Core 的应用

页面的生命周期和程序的生命周期

示例
1、演示如何压缩和解压缩文件
Feature/Compression.xaml

<Page
x:Class="XamlDemo.Feature.Compression"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="Transparent">
<StackPanel Margin="120 0 0 0">

<TextBlock Name="lblMsg" FontSize="14.667" />

<Button Name="btnXpress" Content="Xpress" Margin="0 10 0 0" Click="btnXpress_Click" />

<Button Name="btnXpressHuff" Content="XpressHuff" Margin="0 10 0 0" Click="btnXpressHuff_Click" />

<Button Name="btnMszip" Content="Mszip" Margin="0 10 0 0" Click="btnMszip_Click" />

<Button Name="btnLzms" Content="Lzms" Margin="0 10 0 0" Click="btnLzms_Click" />

</StackPanel>
</Grid>
</Page>


Feature/Compression.xaml.cs

/*
* 演示如何压缩和解压缩文件
*
* 注:对于非常小的数据压缩后可能比压缩前还要大,已经经过压缩算法的文件如 jpg mp3 mp4 等再压缩效果不明显也可能比之前还大
*/

using System;
using Windows.Storage;
using Windows.Storage.Compression;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XamlDemo.Common;

namespace XamlDemo.Feature
{
public sealed partial class Compression : Page
{
public Compression()
{
this.InitializeComponent();
}

private void btnXpress_Click(object sender, RoutedEventArgs e)
{
// XPRESS 算法
CompressionDemo(CompressAlgorithm.Xpress);
}

private void btnXpressHuff_Click(object sender, RoutedEventArgs e)
{
// 具有霍夫曼编码的 XPRESS 算法
CompressionDemo(CompressAlgorithm.XpressHuff);
}

private void btnMszip_Click(object sender, RoutedEventArgs e)
{
// Mszip 算法
CompressionDemo(CompressAlgorithm.Mszip);
}

private void btnLzms_Click(object sender, RoutedEventArgs e)
{
// Lzms 算法
CompressionDemo(CompressAlgorithm.Lzms);
}

private async void CompressionDemo(CompressAlgorithm algorithm)
{
try
{
if (!Helper.EnsureUnsnapped())
return;

// 选择一个准备压缩的文件
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add("*");
var originalFile = await picker.PickSingleFileAsync();
if (originalFile == null)
return;

lblMsg.Text = "选中了文件:" + originalFile.Name;
lblMsg.Text += Environment.NewLine;

var compressedFilename = originalFile.Name + ".compressed"; // 注意:为了有读写 .compressed 文件的权限,需要在 Package.appxmanifest 中新增一个“文件类型关联”声明,并做相关配置
var compressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(compressedFilename, CreationCollisionOption.GenerateUniqueName);
lblMsg.Text += "创建了一个新文件,用于保存压缩后的文件:" + compressedFile.Name;
lblMsg.Text += Environment.NewLine;

using (var originalInput = await originalFile.OpenReadAsync()) // 打开原始文件
using (var compressedOutput = await compressedFile.OpenAsync(FileAccessMode.ReadWrite)) // 打开压缩后的数据的目标文件(目前是一个空文件)
using (var compressor = new Compressor(compressedOutput.GetOutputStreamAt(0), algorithm, 0)) // 实例化 Compressor
{
var bytesCompressed = await RandomAccessStream.CopyAsync(originalInput, compressor); // 将原始数据写入到压缩后的数据的目标文件
lblMsg.Text += "已将原始文件的数据写入到:" + compressedFile.Name;
lblMsg.Text += Environment.NewLine;

var finished = await compressor.FinishAsync(); // 压缩指定文件中的数据
lblMsg.Text += "此文件中的数据已被压缩:" + compressedFile.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "压缩前大小:" + bytesCompressed.ToString() + " - 压缩后大小:" + compressedOutput.Size.ToString();
lblMsg.Text += Environment.NewLine;
}

var decompressedFilename = originalFile.Name + ".decompressed"; // 注意:为了有读写 .decompressed 文件的权限,需要在 Package.appxmanifest 中新增一个“文件类型关联”声明,并做相关配置
var decompressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(decompressedFilename, CreationCollisionOption.GenerateUniqueName);
lblMsg.Text += "创建了一个新文件,用于保存解压缩后的文件:" + decompressedFile.Name;
lblMsg.Text += Environment.NewLine;

using (var compressedInput = await compressedFile.OpenSequentialReadAsync()) // 打开经过压缩的文件
using (var decompressedOutput = await decompressedFile.OpenAsync(FileAccessMode.ReadWrite)) // 打开解压缩后的数据的目标文件(目前是一个空文件)
using (var decompressor = new Decompressor(compressedInput)) // 实例化 Compressor
{
var bytesDecompressed = await RandomAccessStream.CopyAsync(decompressor, decompressedOutput); // 解压缩数据,并将解压缩后的数据保存到目标文件
lblMsg.Text += "文件解压缩完成:" + decompressedFile.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "解压缩后的大小:" + bytesDecompressed.ToString();
lblMsg.Text += Environment.NewLine;
}
}
catch (Exception ex)
{
lblMsg.Text = ex.ToString();
}
}
}
}


2、演示与 Windows 商店相关的操作
Feature/StoreDemo.xaml

<Page
x:Class="XamlDemo.Feature.StoreDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="Transparent">
<StackPanel Margin="120 0 0 0">

<Button Name="btnOpenAppInStore" Content="跳转到 Windows 商店中指定 app 的主页" Click="btnOpenAppInStore_Click" />

<Button Name="btnReviewAppInStore" Content="在 Windows 商店中评论指定的 app" Click="btnReviewAppInStore_Click" Margin="0 10 0 0" />

<Button Name="btnSearchAppInStore" Content="在 Windows 商店中通过关键字搜索 app" Click="btnSearchAppInStore_Click" Margin="0 10 0 0" />

<Button Name="btnUpdateAppInStore" Content="跳转到 Windows 商店中的 app 更新页" Click="btnUpdateAppInStore_Click" Margin="0 10 0 0" />

<TextBlock Name="lblMsg" FontSize="14.667" Text="与购买相关的部分,请参见 code-behind 中的代码" Margin="0 10 0 0" />

</StackPanel>
</Grid>
</Page>


Feature/StoreDemo.xaml.cs

/*
* 演示与 Windows 商店相关的操作
*
* 注:
* 测试时可以使用 CurrentAppSimulator 来模拟购买行为,msdn 上有对应的 sample 可以下载
*/

using System;
using Windows.ApplicationModel.Store;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Feature
{
public sealed partial class StoreDemo : Page
{
public StoreDemo()
{
this.InitializeComponent();
}

private async void btnOpenAppInStore_Click(object sender, RoutedEventArgs e)
{
// 跳转到 Windows 商店中指定 app 的主页(PDP - program display page; PFN - package family name)
await Launcher.LaunchUriAsync(new Uri(@"ms-windows-store:PDP?PFN=10437webabcd.173815756DD78_s2b9shdpk31kj"));
}

private async void btnReviewAppInStore_Click(object sender, RoutedEventArgs e)
{
// 在 Windows 商店中评论指定的 app(PFN - package family name)
await Launcher.LaunchUriAsync(new Uri(@"ms-windows-store:REVIEW?PFN=10437webabcd.173815756DD78_s2b9shdpk31kj"));
}

private async void btnSearchAppInStore_Click(object sender, RoutedEventArgs e)
{
// 在 Windows 商店中通过关键字搜索 app
await Launcher.LaunchUriAsync(new Uri(@"ms-windows-store:Search?query=贪吃蛇"));
}

private async void btnUpdateAppInStore_Click(object sender, RoutedEventArgs e)
{
// 跳转到 Windows 商店中的 app 更新页
await Launcher.LaunchUriAsync(new Uri(@"ms-windows-store:Updates"));
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
CurrentAppDemo();

LicenseInformationDemo();
}

private void CurrentAppDemo()
{
// 由 Windows 商店创建的此 app 的 id
// CurrentApp.AppId;

// 获取此 app 在 Windows 商店中的 URI 地址
// CurrentApp.LinkUri;

// 获取当前 app 的 LicenseInformation 对象
// CurrentApp.LicenseInformation;

// 请求应用程序内购买完整许可证
// CurrentApp.RequestAppPurchaseAsync();

// 请求应用程序内购买指定产品的许可证
// CurrentApp.RequestProductPurchaseAsync

// 获取此 app 的全部购买记录
// CurrentApp.GetAppReceiptAsync();

// 获取此 app 的指定产品的购买记录
// CurrentApp.GetProductReceiptAsync();

// 获取此 app 的 ListingInformation 信息
// CurrentApp.LoadListingInformationAsync();

// ListingInformation - 此 app 在 Windows 商店中的相关信息
//     AgeRating - app 的年龄分级
//     CurrentMarket - app 的当前商店,如:en-us
//     Name - app 在当前商店中的名称
//     FormattedPrice - app 在当前商店中的价格
//     Description - app 在当前商店中的程序说明
//     ProductListings - app 在当前商店中的 ProductListing 集合

// ProductListing - app 的产品信息
//     ProductId - 产品 ID
//     Name - 产品名称
//     FormattedPrice - 产品当前市场的格式化后的价格
}

private void LicenseInformationDemo()
{
// 当前 app 的许可证信息
LicenseInformation licenseInformation = CurrentApp.LicenseInformation;

// 许可证ok则为true,否则无许可证或许可证过期则为false
// licenseInformation.IsActive;

// 是否是试用许可证
// licenseInformation.IsTrial;

// 许可证的到期时间
// licenseInformation.ExpirationDate;

// 许可证状态发生变化时所触发的事件
// licenseInformation.LicenseChanged;

// 获取此 app 相关的 ProductLicenses 集合
// licenseInformation.ProductLicenses;

// ProductLicense - 产品许可证
//     ProductId - 产品 ID
//     IsActive - 此产品许可证ok则为true,否则无此产品许可证或此产品许可证过期则为false
//     ExpirationDate - 此产品许可证的到期时间
}
}
}


3、演示与 Windows 商店相关的操作
WebServer/AppAndWeb.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!--
以下简介如何将网站连接到 Windows 应用商店应用
关于 app 与 web 的更多内容请参见:http://msdn.microsoft.com/zh-cn/library/ie/hh781489(v=vs.85).aspx
-->

<!--包名称,系统会通过此值在本地查找应用,如果有则右下角的按钮会显示“切换到 xxx 应用”,选中后会启动对应的应用-->
<meta name="msApplication-ID" content="microsoft.microsoftskydrive" />

<!--包系列名称,当无法通过包名称查找到本地应用时,右下角的按钮会显示“获取此网站的应用”,选中后会通过包系列名称在 Windows 商店中查找-->
<meta name="msApplication-PackageFamilyName" content="microsoft.microsoftskydrive_8wekyb3d8bbwe" />

<!--
启动对应的应用时,传递过去的参数

app 中通过如下方法获取参数
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (!String.IsNullOrEmpty(args.Arguments))
{

}
}
-->
<meta name="msApplication-Arguments" content="hello webabcd" />

<title>app and web</title>
</head>
<body>
metro ie 打开,可以通过右下角的按钮“获取此网站的应用”或“切换到 xxx 应用”
</body>
</html>


4、演示几个 Core 的应用
Feature/CoreDemo.xaml.cs

/*
* 演示几个 Core 的应用
*/

using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Feature
{
public sealed partial class CoreDemo : Page
{
public CoreDemo()
{
this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
CoreDispatcherDemo();

CoreWindowDemo();

CoreApplicationDemo();
}

private async void CoreDispatcherDemo()
{
// CoreDispatcher - 消息调度器
Windows.UI.Core.CoreDispatcher coreDispatcher = Windows.UI.Xaml.Window.Current.Dispatcher;

await coreDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
// 调用 coreDispatcher 所在的线程
});

coreDispatcher.AcceleratorKeyActivated += (Windows.UI.Core.CoreDispatcher sender, Windows.UI.Core.AcceleratorKeyEventArgs args) =>
{
// 当快捷键被激活(按住)时所触发的事件
};
}

private void CoreWindowDemo()
{
// CoreWindow - 窗口对象,可以监测用户的输入
Windows.UI.Core.CoreWindow coreWindow = Windows.UI.Xaml.Window.Current.CoreWindow;

// coreWindow.KeyDown - 按键按下时触发的事件
// coreWindow.PointerPressed - 指针按下时触发的事件
// coreWindow.GetAsyncKeyState(VirtualKey virtualKey) - 异步返回虚拟键的状态
// coreWindow.GetKeyState(VirtualKey virtualKey) - 返回虚拟键的状态
// coreWindow.Activate(), coreWindow.Close() - 激活窗口, 关闭窗口
// coreWindow.Activated, coreWindow.Closed - 窗口被激活时触发的事件,窗口被关闭时触发的事件
// coreWindow.Dispatcher - 窗口的消息调度器
// coreWindow.Bounds - 窗口的边框
}

private void CoreApplicationDemo()
{
// Windows.ApplicationModel.Core.CoreApplication.Id - 获取 Package.appxmanifest 中 <Application Id="Win8App" /> 所配置的值

// Windows.ApplicationModel.Core.CoreApplication.Exiting - 关闭应用程序时触发的事件
// Windows.ApplicationModel.Core.CoreApplication.Resuming - 继续应用程序时触发的事件
// Windows.ApplicationModel.Core.CoreApplication.Suspending - 挂起应用程序时触发的事件

// Windows.ApplicationModel.Core.CoreApplication.Properties - 一个字典表,用于在应用程序运行时保存和获取全局信息
}
}
}


5、简述页面的生命周期和程序的生命周期
Feature/LifeCycle.xaml.cs

/*
* 页面生命周期:
* 1、导航相关的就是:OnNavigatedTo(), OnNavigatingFrom(), OnNavigatedFrom()
* 2、关于导航的更多内容参见:Controls/Frame/Demo.xaml
* 3、有一点需要注意:Popup 中的内容每次显示时都会触发 Loaded 事件
*
*
* 程序生命周期(参见:App.xaml.cs):
* 1、OnSuspending() 时保存状态
* 2、OnLaunched() 时恢复状态
* 3、Suspending - 转为挂起状态时触发的事件
* 4、Resuming - 由挂起状态转为运行状态时触发的事件
*/

using Windows.UI.Xaml.Controls;

namespace XamlDemo.Feature
{
public sealed partial class LifeCycle : Page
{
public LifeCycle()
{
this.InitializeComponent();
}
}
}


OK
[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐