背水一戰 Windows 10 (99) - 關聯啟動: 關聯指定的檔案類型, 關聯指定的協議

來源:互聯網
上載者:User

標籤:關聯   protected   win10   frame   use   abc   query   soc   send   

[源碼下載]


背水一戰 Windows 10 (99) - 關聯啟動: 關聯指定的檔案類型, 關聯指定的協議



webabcd


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

  • 關聯指定的檔案類型
  • 關聯指定的協議



樣本
1、示範如何關聯指定的檔案類型(即用本程式開啟指定類型的檔案)
App.xaml.cs

        // 通過開啟檔案啟用應用程式時所調用的方法        protected override void OnFileActivated(FileActivatedEventArgs args)        {            Frame rootFrame = new Frame();            rootFrame.Navigate(typeof(Windows10.AssociationLaunching.FileTypeAssociation), args);            Window.Current.Content = rootFrame;            Window.Current.Activate();        }

AssociationLaunching/FileTypeAssociation.xaml

<Page    x:Class="Windows10.AssociationLaunching.FileTypeAssociation"    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 Name="grid" Background="Transparent">                <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5">            <Run>本程式可以開啟 .webabcd 類型的檔案</Run>            <LineBreak />            <Run>測試方法:在案頭建立一個文字檔,隨便輸一些字元,然後將尾碼名改為 .webabcd,最後開啟檔案,本程式會顯示其常值內容</Run>        </TextBlock>            </Grid></Page>

AssociationLaunching/FileTypeAssociation.xaml.cs

/* * 示範如何關聯指定的檔案類型(即用本程式開啟指定類型的檔案) *  * 1、在 Package.appxmanifest 中新增一個“檔案類型關聯”聲明,並做相關配置 *    本例中的這部分的配置如下 *    <uap:Extension Category="windows.fileTypeAssociation"> *      <uap:FileTypeAssociation Name=".webabcd"> *        <uap:DisplayName>開啟 .webabcd 檔案</uap:DisplayName> *        <uap:Logo>Assets\StoreLogo.png</uap:Logo> *        <uap:InfoTip>用 win10 demo 開啟 .webabcd 檔案</uap:InfoTip> *        <uap:SupportedFileTypes> *          <uap:FileType>.webabcd</uap:FileType> *        </uap:SupportedFileTypes> *      </uap:FileTypeAssociation> *    </uap:Extension> * 2、在 App.xaml.cs 中 override void OnFileActivated(FileActivatedEventArgs args),以擷取相關的檔案資訊 *  * FileActivatedEventArgs - 通過開啟檔案啟用應用程式時的事件參數 *     Files - 相關的檔案資訊 *     Kind - 此 app 被啟用的類型(ActivationKind 枚舉) *         比如,如果是通過“開啟檔案”啟用的話,則此值為 File *     PreviousExecutionState - 此 app 被啟用前的狀態(ApplicationExecutionState 枚舉) *         比如,如果此 app 被啟用前就是運行狀態的或,則此值為 Running *     NeighboringFilesQuery - 擷取當前檔案的相鄰檔案 *     SplashScreen - 擷取此 app 的 SplashScreen 對象 *     User - 擷取啟用了此 app 的 User 對象 */using System;using Windows.ApplicationModel.Activation;using Windows.Storage;using Windows.UI;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;namespace Windows10.AssociationLaunching{    public sealed partial class FileTypeAssociation : Page    {        private FileActivatedEventArgs _fileActivated;        public FileTypeAssociation()        {            this.InitializeComponent();        }        protected async override void OnNavigatedTo(NavigationEventArgs e)        {            // 擷取 FileActivatedEventArgs 對象(從 App.xaml.cs 傳來的)            _fileActivated = e.Parameter as FileActivatedEventArgs;            // 擷取檔案中的常值內容,並顯示            if (_fileActivated != null)            {                grid.Background = new SolidColorBrush(Colors.Blue);                lblMsg.Foreground = new SolidColorBrush(Colors.White);                IStorageFile isf = _fileActivated.Files[0] as IStorageFile;                lblMsg.Text = $"啟用程式的檔案是“{isf.Name}”,其常值內容為:{await FileIO.ReadTextAsync(isf)}";            }        }    }}


2、示範如何關聯指定的協議(即用本程式處理指定的協議)
App.xaml.cs

        protected override void OnActivated(IActivatedEventArgs args)        {            // 通過協議啟用應用程式時(參見 AssociationLaunching/ProtocolAssociation.xaml.cs 中的樣本)            if (args.Kind == ActivationKind.Protocol)            {                ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;                Frame rootFrame = new Frame();                rootFrame.Navigate(typeof(Windows10.AssociationLaunching.ProtocolAssociation), protocolArgs);                Window.Current.Content = rootFrame;            }        }

AssociationLaunching/ProtocolAssociation.xaml

<Page    x:Class="Windows10.AssociationLaunching.ProtocolAssociation"    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 Name="grid" Background="Transparent">                <StackPanel Margin="10 0 10 10">            <TextBlock Name="lblMsg" Margin="5">                <Run>本程式可以處理 webabcd: 協議</Run>            </TextBlock>            <Button Name="btnProtocol" Content="開啟自訂協議 webabcd:data" Click="btnProtocol_Click" Margin="5" />        </StackPanel>            </Grid></Page>

AssociationLaunching/ProtocolAssociation.xaml.cs

/* * 示範如何關聯指定的協議(即用本程式處理指定的協議) *  * 1、在 Package.appxmanifest 中新增一個“協議”聲明,並做相關配置 *    本例中的這部分的配置如下,協議名必須全小寫 *    <uap:Extension Category="windows.protocol"> *      <uap:Protocol Name="webabcd"> *        <uap:Logo>Assets\StoreLogo.png</uap:Logo> *      </uap:Protocol> *    </uap:Extension> * 2、在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args),以擷取相關的協議資訊 *  *  * ProtocolActivatedEventArgs - 通過協議啟用應用程式時的事件參數 *     Uri - 協議的 uri *     CallerPackageFamilyName - 啟用當前 app 的 app 的 PackageFamilyName *     Kind - 此 app 被啟用的類型(ActivationKind 枚舉) *         本例為 ActivationKind.Protocol *     PreviousExecutionState - 此 app 被啟用前的狀態(ApplicationExecutionState 枚舉) *         比如,如果此 app 被啟用前就是運行狀態的或,則此值為 Running *     SplashScreen - 擷取此 app 的 SplashScreen 對象 *     User - 擷取啟用了此 app 的 User 對象 */using System;using Windows.ApplicationModel.Activation;using Windows.System;using Windows.UI;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;namespace Windows10.AssociationLaunching{    public sealed partial class ProtocolAssociation : Page    {        private ProtocolActivatedEventArgs _protocolArgs;        public ProtocolAssociation()        {            this.InitializeComponent();        }        protected override void OnNavigatedTo(NavigationEventArgs e)        {            // 擷取 ProtocolActivatedEventArgs 對象(從 App.xaml.cs 傳來的)            _protocolArgs = e.Parameter as ProtocolActivatedEventArgs;            // 顯示協議的詳細資料            if (_protocolArgs != null)            {                grid.Background = new SolidColorBrush(Colors.Blue);                lblMsg.Foreground = new SolidColorBrush(Colors.White);                lblMsg.Text = "啟用程式的自訂協議為: " + _protocolArgs.Uri.AbsoluteUri;            }        }        private async void btnProtocol_Click(object sender, RoutedEventArgs e)        {            // 開啟自訂協議 webabcd:data            Uri uri = new Uri("webabcd:data");            await Launcher.LaunchUriAsync(uri);            // 開啟 IE 瀏覽器,在地址欄輸入 webabcd:data,即會開啟本程式來處理此協議        }    }}



OK
[源碼下載]

背水一戰 Windows 10 (99) - 關聯啟動: 關聯指定的檔案類型, 關聯指定的協議

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.