WP8文件关联

来源:互联网 发布:javascript脚本引擎 编辑:程序博客网 时间:2024/06/05 06:18
当用户想要打开某个特定文件时,文件关联允许您的应用自动启动。该文件可能来自不同的来源,这包括但不限于以下来源:
电子邮件附件
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);
}
0 0
原创粉丝点击