个人资料

跳过导航链接首页 > 博客列表 > 博客正文

UWP使用文件关联

:

告诉系统APP能打开XXX类型的文件

在Package.appxmanifest文件选择声明->支持的文件类型->右侧修改关联文件类型。

或者资源管理器中Package.appxmanifest->右键,打开方式,xml编辑器打开->找到applications节点下application节点,在该节点下添加:

<Extensions>
  <uap:Extension Category="windows.fileTypeAssociation">
    <uap:FileTypeAssociation Name=".txt">
      <uap:SupportedFileTypes>
        <uap:FileType>.txt</uap:FileType>
        <uap:FileType>.rtf</uap:FileType>
      </uap:SupportedFileTypes>
    </uap:FileTypeAssociation>
  </uap:Extension>
</Extensions>


上面例子添加了txt格式和rtf格式的文件类型关联(修改xml和点击修改效果相同)。用户具有选择文件打开方式的自由,通过上述操作用户在选择文件打开方式的时候,app会出现在默认列表中。

当用户通过打开文件激活app时获取并处理文件

重写OnFileActivated方法,该方法在用户通过打开文件激活app时发生,用户有可能一次性选择多个文件打开,因此args.Files是一个StorageFile数组。我们获取StorageFile就获得了打开文件的全部信息。然后导航到页面中,将file以参数的形式传递。在具体的页面中OnNavigateTo事件中处理


protected override void OnFileActivated(FileActivatedEventArgs args)
   {
       StorageFile file = args.Files[0] as StorageFile;
       Frame rootFrame = Window.Current.Content as Frame;
       if (rootFrame==null)
       {
           rootFrame = new Frame();
           Window.Current.Content = rootFrame;
       }
       rootFrame.BackStack.Clear();
        
       rootFrame.Navigate(typeof(MainPage),file);
       Window.Current.Activate();
       base.OnFileActivated(args);
   }


songshizhao
最初发表2017/12/1 0:55:23 最近更新2017/12/1 0:55:23 4676
为此篇作品打分
10