Windows 8 Store Apps學習(33) 關聯啟動

來源:互聯網
上載者:User

關聯啟動: 使用外部程式開啟一個檔案或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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.