Windows 8 Store Apps学习(33) 关联启动2013-12-06 cnblogs webabcd关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议介绍重新想象 Windows 8 Store Apps 之 关联启动使用外部程序打开一个文件使用外部程序打开一个 Uri关联指定的文件类型(即用本程序打开指定类型的文件)关联指定的协议(即用本程序处理指定的协议)示例1、演示如何使用外部程序打开一个文件AssociationLaunching/LaunchFile.xaml
<Pagex:Class="XamlDemo.AssociationLaunching.LaunchFile"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Launcher"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" Margin="0 0 0 10" /><RadioButton Content="使用默认打开方式打开文件" Name="radDefault" GroupName="LaunchType" IsChecked="True" /><RadioButton Content="使用默认打开方式打开文件,打开前弹出警告框" Name="radWarning" GroupName="LaunchType" /><RadioButton Content="选择指定的打开方式打开文件" Name="radOpenWith" GroupName="LaunchType" /><Button Content="打开一个 .png 格式文件" Name="btnLaunchFile" Click="btnLaunchFile_Click_1" Margin="0 10 0 0" /></StackPanel></Grid></Page>
AssociationLaunching/LaunchFile.xaml.cs
/* * 演示如何使用外部程序打开一个文件 */using System;using Windows.Foundation;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.AssociationLaunching{public sealed partial class LaunchFile : Page{public LaunchFile(){this.InitializeComponent();}private async void btnLaunchFile_Click_1(object sender, RoutedEventArgs e){/* * Launcher - 用于启动与指定文件相关的应用程序 * LaunchFileAsync(IStorageFile file) - 打开指定的文件 * LaunchFileAsync(IStorageFile file, LauncherOptions options) - 打开指定的文件 ** LauncherOptions - 启动外部应用程序时的相关选项 * TreatAsUntrusted - 使用默认应用程序打开指定的文件时,是否弹出安全警告 * DisplayApplicationPicker - 是否弹出“打开方式”对话框 * UI.InvocationPoint - 指定“打开方式”对话框的显示位置 ** 当指定的文件不被任何应用程序支持时,可以用以下下两种方法处理 * 1、指定 LauncherOptions.FallbackUri: 打开浏览器并跳转到指定的地址 * 2、指定 PreferredApplicationDisplayName 和 PreferredApplicationPackageFamilyName *PreferredApplicationDisplayName - 指定在弹出的“在商店搜索”对话框中所显示的应用程序名称 *PreferredApplicationPackageFamilyName - 用户点击“在商店搜索”后,会在商店搜索指定 PackageFamilyName */// 指定需要打开的文件var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"AssetsLogo.png");// 指定打开文件过程中相关的各种选项var options = new Windows.System.LauncherOptions();if (radWarning.IsChecked.Value){options.TreatAsUntrusted = true;}if (radOpenWith.IsChecked.Value){Point openWithPosition = GetOpenWithPosition(btnLaunchFile);options.DisplayApplicationPicker = true;options.UI.InvocationPoint = openWithPosition;}// 使用外部程序打开指定的文件bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);if (success){lblMsg.Text = "打开成功";}else{lblMsg.Text = "打开失败";}}// 获取“打开方式”对话框的显示位置,即关联 Button 的左下角点的坐标private Windows.Foundation.Point GetOpenWithPosition(FrameworkElement element){Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);Point desiredLocation = buttonTransform.TransformPoint(new Point());desiredLocation.Y = desiredLocation.Y + element.ActualHeight;return desiredLocation;}}}
2、演示如何使用外部程序打开指定的 UriAssociationLaunching/LaunchUri.xaml
<Pagex:Class="XamlDemo.AssociationLaunching.LaunchUri"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Launcher"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" Margin="0 0 0 10" /><RadioButton Content="使用默认打开方式打开指定的 Uri" Name="radDefault" GroupName="LaunchType" IsChecked="True" /><RadioButton Content="使用默认打开方式打开指定的 Uri,打开前弹出警告框" Name="radWarning" GroupName="LaunchType" /><RadioButton Content="选择指定的打开方式打开指定的 Uri" Name="radOpenWith" GroupName="LaunchType" /><Button Content="打开一个 uri" Name="btnLaunchUri" Click="btnLaunchUri_Click_1" Margin="0 10 0 0" /></StackPanel></Grid></Page>