[源码下载]

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

作者: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(), algorithm, )) // 实例化 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
[源码下载]

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

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

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

  2. Windows 8 Store Apps

    重新想象 Windows 8 Store Apps 系列文章索引 Posted on 2013-11-18 08:33 webabcd 阅读(672) 评论(3) 编辑 收藏 [源码下载] 重新想象 ...

  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. Openwrt安装软件的方法

    转自:http://blog.csdn.net/jk110333/article/details/8753825     web软件: Naken Web Openwrt安装软件 简介 Openwrt ...

  2. Struts2返回json

    Action怎么返回json类型数据?方法1,使用struts2的插件struts2-json-plugin-2.3.8.jar(在下载的strut2库文件夹中). 在struts2.xml中对要返回 ...

  3. Arduino 学习

    Arduino 背景可以参考官方网站www.arduino.cc 先看一个最简单的示例程序: 打开 Arduino IDE , 选择菜单:文件 -> 示例 -> 01.Basics -&g ...

  4. Etl之HiveSql调优(left join where的位置)

    一.前言 公司实用Hadoop构建数据仓库,期间不可避免的实用HiveSql,在Etl过程中,速度成了避无可避的问题.本人有过几个数据表关联跑1个小时的经历,你可能觉得无所谓,可是多次Etl就要多个小 ...

  5. Android 模拟器 获得 root权限

    启动一个模拟器,开始-运行-输入cmd,打开dos,依次输入 adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ...

  6. Unix sed实用教程开篇

    已经看了一段时间的Linux Shell编程了,也能完成一些基本的使用,为了加深理解,恰好看到了The Unix School的一个sed&awk教程,不是简单的命令参数堆积,而是一个相当实用 ...

  7. 在使用sqlite时淌过的坑

    以前一直用sqlite.net 1.0.66.0版本,在.net4下面程序写好了部署到目的地机器时winform程序总是出现缺少运行时的问题.有时装了运行时也还是出问题,后来发现是混合模式的问题,当时 ...

  8. ext 3.x 让uploadPanel支持swfupload

    经常做系统的时候会遇到上传组件,特别是大文件的时候总是很郁闷,长时间无响应导致糟糕的用户体验,所以决定采用swfupload来支持文件上传. 大体代码如下. var upload = {}; uplo ...

  9. 【PRML读书笔记-Chapter1-Introduction】1.5 Decision Theory

    初体验: 概率论为我们提供了一个衡量和控制不确定性的统一的框架,也就是说计算出了一大堆的概率.那么,如何根据这些计算出的概率得到较好的结果,就是决策论要做的事情. 一个例子: 文中举了一个例子: 给定 ...

  10. cocos2d-x开发: 场景实体(entity)管理

    公司现在开新项目,主题的框架部分都是我自己在做,不用受到别人的牵制,所以还算是比较的自由,很好发挥. 游戏并不大,所以需要用到的地方并不多.今天花了一些时间写了场景entity管理的部分代码,还没有完 ...