天天看點

WP8檔案關聯

當使用者想要打開某個特定檔案時,檔案關聯允許您的應用自動啟動。該檔案可能來自不同的來源,這包括但不限于以下來源:

電子郵件附件

Internet Explorer 中的網站

近距離無線通信 (NFC) 标記

商店中的其他應用

檔案關聯還可以用來确定,通過使用外部存儲 API,應用可以從 SD 卡讀取哪些檔案類型。有關這些 API 的更多資訊,請參見 Windows Phone 8 的資料。

注冊檔案關聯

要處理特定的檔案類型,請在應用清單檔案中注冊檔案關聯。應用能夠指定任何可處理的檔案關聯。但是,任何由内置應用保留的檔案關聯将被忽略。

有關更多資訊,請參見 Windows Phone 8 的保留檔案和 URI 關聯。

注意:有時 Internet Explorer 重寫檔案關聯:它啟動内置的媒體播放器來處理所有的音樂和視訊檔案類型。具體來說,具有 audio/ 或 video/ 内容類型的任何檔案。

例如,應用從電子郵件附件啟動時,它可以處理(非保留的)音樂和視訊檔案。但是如果從 Internet Explorer 啟動相同的檔案,則内置的媒體播放器将處理該檔案。

要注冊檔案關聯,您必須使用 XML(文本)編輯器編輯 WMAppManifest.xml。在“解決方案資料總管”中,右鍵單擊 WMAppManifest.xml 檔案,然後單擊“打開方式”。

在“打開方式”視窗中,選擇“XML(文本)編輯器”,然後單擊“确定”。在應用清單檔案的 Extensions 元素中,檔案關聯是使用 FileTypeAssociation 元素指定的。

請注意,Extensions 元素必須緊跟在 Tokens 元素之後。下面的示例示範了稱為“Windows Phone SDK 測試檔案類型”的假想檔案類型的檔案關聯,該關聯能夠處理兩種不同檔案擴充名: 

<Extensions>

   <FileTypeAssociation Name="Windows Phone SDK test file type" TaskID="_default" NavUriFragment="fileToken=%s">

       <Logos>

           <Logo Size="small" IsRelative="true">Assets/sdk-small-33x33.png</Logo>

           <Logo Size="medium" IsRelative="true">Assets/sdk-medium-69x69.png</Logo>

           <Logo Size="large" IsRelative="true">Assets/sdk-large-176x176.png</Logo>

       </Logos>

       <SupportedFileTypes>

         <FileType ContentType="application/sdk">.sdkTest1</FileType>

         <FileType ContentType="application/sdk">.sdkTest2</FileType>

       </SupportedFileTypes>

   </FileTypeAssociation>

</Extensions>

偵聽檔案啟動

當您的應用啟動以處理某個特定的檔案類型時,深層連結 URI 用于将使用者帶到您的應用。在 URI 中,FileTypeAssociation 字元串指定 URI 的源為檔案關聯且 fileToken 參數包含檔案标記。

例如,以下代碼顯示了檔案關聯的深層連結 URI。

/FileTypeAssociation?fileToken=89819279-4fe0-4531-9f57-d633f0949a19

啟動時,将傳入的深層連結 URI 映射到能夠處理該檔案的應用頁面。如果您擁有用于處理多個檔案類型的多個頁面,請在映射 URI 之前使用自定義的 URI 映射器和 GetSharedFileName 方法檢查檔案類型。

例如,以下代碼示範了用于分析深層連結 URI 并根據檔案的類型映射不同頁面的 URI 映射器。如果映射器不是從檔案關聯啟動的,它會在保持 URI 不變的情況下,将完整的 URI 字元串發回到 App 對象。

using System;

using System.IO;

using System.Windows.Navigation;

using Windows.Phone.Storage.SharedAccess;

namespace sdkAutoLaunch

{

    class AssociationUriMapper : UriMapperBase

    {

        private string tempUri;

        public override Uri MapUri(Uri uri)

        {

            tempUri = uri.ToString();

            // File association launch

            if (tempUri.Contains("/FileTypeAssociation"))

            {

                // Get the file ID (after "fileToken=").

                int fileIDIndex = tempUri.IndexOf("fileToken=") + 10;

                string fileID = tempUri.Substring(fileIDIndex);

                // Get the file name.

                string incomingFileName =

                    SharedStorageAccessManager.GetSharedFileName(fileID);

                // Get the file extension.

                string incomingFileType = Path.GetExtension(incomingFileName);

                // Map the .sdkTest1 and .sdkTest2 files to different pages.

                switch (incomingFileType)

                {

                    case ".sdkTest1":

                        return new Uri("/sdkTest1Page.xaml?fileToken=" + fileID, UriKind.Relative);

                    case ".sdkTest2":

                        return new Uri("/sdkTest2Page.xaml?fileToken=" + fileID, UriKind.Relative);

                    default:

                        return new Uri("/MainPage.xaml", UriKind.Relative);

                }

            }

            // Otherwise perform normal launch.

            return uri;

        }

    }

}

在此類情況下,若要在您的應用中使用 URI 映射器類,請将其配置設定到該應用在 App.xaml.cs 檔案中所對應的架構。在 InitializePhoneApplication 方法中,在 RootFrame.Navigated 已被配置設定後,緊接着将 RootFrame.UriMapper 屬性設定為與您的 URI 映射器類相等。在下列示例中,AssociationUriMapper 類被配置設定給架構的 UriMapper 屬性。

C#

private void InitializePhoneApplication()

{

    if (phoneApplicationInitialized)

        return;

    // Create the frame but don't set it as RootVisual yet; this allows the splash

    // screen to remain active until the application is ready to render.

    RootFrame = new PhoneApplicationFrame();

    RootFrame.Navigated += CompleteInitializePhoneApplication;

    // Assign the URI-mapper class to the application frame.

    RootFrame.UriMapper = new AssociationUriMapper();

    // Handle navigation failures

    RootFrame.NavigationFailed += RootFrame_NavigationFailed;

    // Ensure we don't initialize again

    phoneApplicationInitialized = true;

}

啟動應用後,它将在初始化期間配置設定 URI 映射器。在啟動任何頁面之前,應用将調用 URI 映射器的 MapUri 方法确定要啟動的頁面。URI 映射器傳回的 URI 即應用所啟動的頁面。

啟動頁面時,通過使用頁面的 NavigationContext 對象的 QueryString 屬性,頁面可以通路 URI(啟動了該頁面)中的所有參數。例如,以下代碼将所有的 URI 參數和值都放置在 IDictionary 對象中。

IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

檢索檔案

當您獲得了深層連結 URI 的檔案标記後,使用 Windows.Phone.Storage.SharedAccess 命名空間中的 SharedStorageAccessManager 靜态類通路該檔案。SharedStorageAccessManager 提供了以下方法。

方法                  傳回類型                                描述

GetSharedFileName     System.String                 傳回檔案名(包括檔案擴充名)。

CopySharedFileAsync   Windows.Storage.StorageFile  将檔案複制到特定的位置并傳回副本。

啟動檔案

如前所述,您的應用也可以啟動檔案,以便另一個應用能夠打開它。為此,請使用 Windows.System 命名空間的啟動器對象的 LaunchFileAsync 方法。例如,下列代碼從本地存儲啟動了一個假想 Contoso Bug 查詢檔案。

private async void LaunchFileButton_Click(object sender, RoutedEventArgs rea)

{

    // Access isolated storage.

    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    // Access the bug query file.

    StorageFile bqfile = await local.GetFileAsync("file1.bqy");

    // Launch the bug query file.

    Windows.System.Launcher.LaunchFileAsync(bqfile);

}

wp8