Javascript Windows.System.Launcher.launchFileAsync(文件)不工作:(

Javascript Windows.System.Launcher.launchFileAsync(文件)不工作:(,javascript,api,sdk,windows-8,launcher,Javascript,Api,Sdk,Windows 8,Launcher,我试着使用JSAPI开发Windows8应用商店应用程序。基本上我有两个按钮,一个调用函数create(),另一个调用函数open()。 文件创建正确,但我无法想象如何启动外部应用程序来打开它 鉴于此代码主要取自MSDN网站……我应该写什么来代替: //如何启动应用程序来读取文件 该文件存在,我可以通过var file=Windows.Storage.StorageFile.getFileFromApplicationUrisync(uri)获取其引用 但即使是Windows.System.La

我试着使用JSAPI开发Windows8应用商店应用程序。基本上我有两个按钮,一个调用函数create(),另一个调用函数open()。 文件创建正确,但我无法想象如何启动外部应用程序来打开它

鉴于此代码主要取自MSDN网站……我应该写什么来代替:

//如何启动应用程序来读取文件

该文件存在,我可以通过var file=Windows.Storage.StorageFile.getFileFromApplicationUrisync(uri)获取其引用

但即使是Windows.System.Launcher.launchFileAsync(文件)也无法工作…:( 有什么帮助吗

谢谢

 function create() {

 {
        localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
           .then(function (sampleFile) {
               var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
               var timestamp = formatter.format(new Date());

               return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
           }).done(function () {



           });
    }




        }


function open() {



    localFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {

          if (sampleFile) { console.log("it does exists") }

          return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
          var uri = new Windows.Foundation.Uri('ms-appx:///dataFile.txt');
          var file = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);
          //how to launch an app to read the file?
      }, function () {
          console.log("timestamp non trovato");
      });

}

这里有几件事:

  • 您正在对属于应用程序一部分的文件使用URI方案,而不是用于本地文件夹的文件。请改用此方案

    var uri = new Windows.Foundation.Uri('ms-appdata:///local/dataFile.txt');
    
  • 您需要使用来自的回调,请尝试:

  • 您实际上不需要在这里创建Uri,您可以在第一个“then”中启动(但我不确定您的意图,因为您也在那里读取了文件)。还要注意,您对现有文件的检查不太正确。如果该文件不存在,您将触发错误处理程序回调,我在下面添加了该回调以输出到控制台

    localFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
               Windows.System.Launcher.launchFileAsync(sampleFile);
               return Windows.Storage.FileIO.readTextAsync(sampleFile);
           },
           function () { console.log("it does not exist"); })
      .done(function (timestamp) {
    
          // not sure what you want to do here
    
      }, function () {
          console.log("timestamp non trovato");
      }
    );});
    

  • 谢谢你,吉姆。我检查了你的答案是否正确。你太好了。谢谢你抽出时间!
    localFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
               Windows.System.Launcher.launchFileAsync(sampleFile);
               return Windows.Storage.FileIO.readTextAsync(sampleFile);
           },
           function () { console.log("it does not exist"); })
      .done(function (timestamp) {
    
          // not sure what you want to do here
    
      }, function () {
          console.log("timestamp non trovato");
      }
    );});