關聯啟動: 使用外部程式開啟一個檔案或uri, 關聯指定的檔案類型或協議
介紹
重新想象 Windows 8 Store Apps 之 關聯啟動
使用外部程式開啟一個檔案
使用外部程式開啟一個 Uri
關聯指定的檔案類型(即用本程式開啟指定類型的檔案)
關聯指定的協議(即用本程式處理指定的協議)
樣本
1、示範如何使用外部程式開啟一個檔案
AssociationLaunching/LaunchFile.xaml
<Page x: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(@"Assets\Logo.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、示範如何使用外部程式開啟指定的 Uri
AssociationLaunching/LaunchUri.xaml
<Page x: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>