重新想象 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 的应用, 页面的生命周期和程序的生命周期的更多相关文章
- 重新想象 Windows 8 Store Apps 系列文章索引
[源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- Windows 8 Store Apps
重新想象 Windows 8 Store Apps 系列文章索引 Posted on 2013-11-18 08:33 webabcd 阅读(672) 评论(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 ...
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解
[源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract
[源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...
- 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract
[源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...
- 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件
[源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...
随机推荐
- 如何导入大sql文件到虚拟主机mysql数据库
大部分网站虚拟主机为了安全起见,都限制了通过命令或者phpMyAdmin导入大sql文件到mysql数据库,例如godaddy只允许站长通过phpMyAdmin上传不超过2m的sql文件,但实际上我们 ...
- LeetCode: Convert Sorted List to Binary Search Tree 解题报告
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- PHP 中 Orientation 属性判断上传图片是否需要旋转(转)
<?php $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name'])); $ex ...
- Spring mvc4 + ActiveMQ 整合
一.配置部分 二.代码部分 三.页面部分 四.Controller控制器 五.效果展示 六.加入监听器 七.最最重要的,别忘了打赏 一.配置部分 ActiveMQ的安装这就不说了,很简单, 这个例子采 ...
- 向上下左右不间断无缝滚动图片的效果(兼容火狐和IE)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MAC自带的SVN进行升级
1.下载高版本svn:http://www.wandisco.com/subversion/download 2.安装 3. #1.在.bash_profile添加export PATH=/opt/s ...
- CLR via C#深解笔记六 - 泛型
面向对象编程一个好处就是“代码重用”,极大提高了开发效率.如是,可以派生出一个类,让它继承基类的所有能力,派生类只需要重写虚方法,或添加一些新的方法,就可以定制派生类的行为,使之满足开发人员的需求. ...
- 读书笔记_Effective_C++_条款四十七:请使用trait classes来表示类型信息
这一条款主要来讨论模板中迭代器的属性iterator_category,它可以通过类似于vector<int>::iterator::iterator_category的方式来取得. 到这 ...
- Go加密解密之RSA[转]
安全总是很重要的,各个语言对于通用的加密算法都会有实现.前段时间,用Go实现了RSA和DES的加密解密,在这分享一下.(对于RSA和DES加密算法本身,请查阅相关资料) 在PHP中,很多功能经常是一个 ...
- Windows Service插件服务开源
WindowsService 插件服务是一个为简化NTService开发和打包程序,提供插件开发的方式进行动态加入或删除业务. 插件式服务程序的由来,在系统维护的过程中,根据企业的要求经常要进行一些周 ...