如何在Windows Store / WinJS应用程序中处理文件激活

How to handle File Activation in Windows Store / WinJS apps

本文关键字:应用程序 处理 文件 激活 WinJS Windows Store      更新时间:2023-09-26

我正在尝试让我的文本编辑器应用程序处理文件启动。Microsoft在此处提供了如何执行此操作的示例:

http://msdn.microsoft.com/en-us/library/windows/apps/hh452684.aspx

不幸的是,它在接收文件时停止,并且没有提供有关如何实际打开该文件的任何信息。

我可以成功处理激活的事件,最终得到文件的绝对路径。例如

C:'Users'Rory'Documents'test.txt

Metro 应用无权访问绝对路径,除非在某些情况下。

    如果用户通过文件
  1. 选取器选择文件
  2. 如果应用之前访问过该文件,并且路径已存储在 Windows.Storage.AccessCache 中
  3. 如果应用正在作为启动传递文件。

即使数字 3 在这种情况下适用,我也无法打开该文件。

我已经尝试过Windows.Storage.StorageFile.getFileFromPathAsync(path_to_file)但是我收到此错误

0x80070005 - JavaScript runtime error: Access is denied.
WinRT information: Cannot access the specified file or folder (඀6). 
The item is not in a location that the application has access to (including 
application data folders, folders that are accessible via capabilities 
and persisted items in the StorageApplicationPermissions lists). Verify 
that the file is not marked with system or hidden file attributes.

我已将应用包清单设置为接受 txt 文件。

StorageFileStorageFileWebUIFileActivatedEventArgs 参数中传递给应用。试试这个:

app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.file) {
        if (args.detail.files.size > 0) {
            var storageFile = args.detail.files[0];
            Windows.Storage.FileIO.readTextAsync(storageFile).then(function (text) {
                // Do something with the content of the file.
            });
        }
    }
    // ...
}