背水一戰 Windows 10 (98) - 關聯啓動: 使用外部程序打開一個文件, 使用外部程序打開一個 Uri

[源碼下載]


html

背水一戰 Windows 10 (98) - 關聯啓動: 使用外部程序打開一個文件, 使用外部程序打開一個 Uri



做者:webabcd


介紹
背水一戰 Windows 10 之 關聯啓動html5

  • 使用外部程序打開一個文件
  • 使用外部程序打開一個 Uri



示例
一、演示如何使用外部程序打開一個文件
AssociationLaunching/LaunchFile.xamlc++

<Page
    x:Class="Windows10.AssociationLaunching.LaunchFile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.AssociationLaunching"
    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="10 0 10 10">
            
            <TextBlock Name="lblMsg" Margin="5" />

            <RadioButton Content="使用默認打開方式打開文件" Name="radDefault" GroupName="LaunchType" IsChecked="True" />
            <RadioButton Content="使用默認打開方式打開文件,打開前彈出警告框" Name="radWarning" GroupName="LaunchType" />
            <RadioButton Content="選擇指定的打開方式打開文件" Name="radOpenWith" GroupName="LaunchType" />

            <Button Content="打開一個 .jpg 格式文件" Name="btnLaunchFile" Click="btnLaunchFile_Click" Margin="5" />
            
        </StackPanel>
    </Grid>
</Page>

AssociationLaunching/LaunchFile.xaml.csweb

/*
 * 演示如何使用外部程序打開一個文件
 * 
 * Launcher - 用於啓動與指定文件相關的應用程序
 *     LaunchFileAsync(IStorageFile file) - 打開指定的文件
 *     LaunchFileAsync(IStorageFile file, LauncherOptions options) - 打開指定的文件
 * 
 * LauncherOptions - 啓動外部應用程序時的相關選項
 *     TreatAsUntrusted - 使用默認應用程序打開指定的文件時,是否彈出安全警告
 *     DisplayApplicationPicker - 是否彈出「打開方式」對話框
 *     UI.InvocationPoint - 指定「打開方式」對話框的顯示位置
 *     
 * 當指定的文件不被任何應用程序支持時,能夠用如下下兩種方法處理
 * 一、指定 LauncherOptions.FallbackUri: 打開瀏覽器並跳轉到指定的地址
 * 二、指定 LauncherOptions.PreferredApplicationDisplayName 和 LauncherOptions.PreferredApplicationPackageFamilyName
 *    PreferredApplicationDisplayName - 指定在彈出的「在商店搜索」對話框中所顯示的應用程序名稱
 *    PreferredApplicationPackageFamilyName - 用戶點擊「在商店搜索」後,會在商店搜索指定 PackageFamilyName
 */

using System;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows10.AssociationLaunching
{
    public sealed partial class LaunchFile : Page
    {
        public LaunchFile()
        {
            this.InitializeComponent();
        }

        private async void btnLaunchFile_Click(object sender, RoutedEventArgs e)
        {
            // 指定須要打開的文件
            StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\hololens.jpg");

            // 指定打開文件過程當中相關的各類選項
            LauncherOptions options = new 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 Launcher.LaunchFileAsync(file, options);
            if (success)
            {
                lblMsg.Text = "打開成功";
            }
            else
            {
                lblMsg.Text = "打開失敗";
            }
        }

        // 獲取「打開方式」對話框的顯示位置,即關聯 Button 的左下角點的座標
        private Point GetOpenWithPosition(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);

            Point desiredLocation = buttonTransform.TransformPoint(new Point());
            desiredLocation.Y = desiredLocation.Y + element.ActualHeight;

            return desiredLocation;
        }
    }
}


二、演示如何使用外部程序打開指定的 Uri
AssociationLaunching/LaunchUri.xamlexpress

<Page
    x:Class="Windows10.AssociationLaunching.LaunchUri"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.AssociationLaunching"
    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="10 0 10 10">
            <TextBlock Name="lblMsg" Margin="5" />

            <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" Margin="5" />
            
        </StackPanel>
    </Grid>
</Page>

AssociationLaunching/LaunchUri.xaml.cswindows

/*
 * 演示如何使用外部程序打開指定的 Uri
 * 
 * Launcher - 用於啓動與指定 Uri 相關的應用程序
 *     LaunchUriAsync(Uri uri) - 打開指定的 Uri
 *     LaunchUriAsync(Uri uri, LauncherOptions options) - 打開指定的 Uri
 *     LaunchUriForResultsAsync(Uri uri, LauncherOptions options, ValueSet inputData) - 打開指定的 Uri,並能夠獲取到被激活的 app 返回的數據(參見 App2AppCommunication/LaunchUriForResults.xaml.cs)
 * 
 * LauncherOptions - 啓動外部應用程序時的相關選項
 *     TreatAsUntrusted - 使用默認應用程序打開指定的文件時,是否彈出安全警告
 *     DisplayApplicationPicker - 是否彈出「打開方式」對話框
 *     UI.InvocationPoint - 指定「打開方式」對話框的顯示位置
 *     TargetApplicationPackageFamilyName - 若是要指定必須用某一目標程序打開的話,就指定此目標程序的 PackageFamilyName
 *     
 * 當指定的 Uri 不被任何應用程序支持時,能夠用如下下兩種方法處理
 * 一、指定 LauncherOptions.FallbackUri: 打開瀏覽器並跳轉到指定的地址
 * 二、指定 LauncherOptions.PreferredApplicationDisplayName 和 LauncherOptions.PreferredApplicationPackageFamilyName
 *    PreferredApplicationDisplayName - 指定在彈出的「在商店搜索」對話框中所顯示的應用程序名稱
 *    PreferredApplicationPackageFamilyName - 用戶點擊「在商店搜索」後,會在商店搜索指定 PackageFamilyName
 */

using System;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows10.AssociationLaunching
{
    public sealed partial class LaunchUri : Page
    {
        public LaunchUri()
        {
            this.InitializeComponent();
        }

        private async void btnLaunchUri_Click(object sender, RoutedEventArgs e)
        {
            // 指定須要打開的 Uri
            Uri uri = new Uri("http://webabcd.cnblogs.com");

            // 指定打開 Uri 過程當中相關的各類選項
            LauncherOptions options = new LauncherOptions();
            if (radWarning.IsChecked.Value)
            {
                options.TreatAsUntrusted = true;
            }
            if (radOpenWith.IsChecked.Value)
            {
                Point openWithPosition = GetOpenWithPosition(btnLaunchUri);

                options.DisplayApplicationPicker = true;
                options.UI.InvocationPoint = openWithPosition;
            }

            // 使用外部程序打開指定的 Uri
            bool success = await Launcher.LaunchUriAsync(uri, options);
            if (success)
            {
                lblMsg.Text = "打開成功";
            }
            else
            {
                lblMsg.Text = "打開失敗";
            }
        }

        // 獲取「打開方式」對話框的顯示位置,即關聯 Button 的左下角點的座標
        private Point GetOpenWithPosition(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);

            Point desiredLocation = buttonTransform.TransformPoint(new Point());
            desiredLocation.Y = desiredLocation.Y + element.ActualHeight;

            return desiredLocation;
        }
    }
}



OK
[源碼下載]瀏覽器

相關文章
相關標籤/搜索