【问题标题】:Prevent winjs application from crashing when file call not succesfull文件调用不成功时防止windows应用程序崩溃
【发布时间】:2015-08-10 18:19:58
【问题描述】:

我目前正在开发适用于平板电脑的 Windows 8.1 应用程序。我想在单击时从用户驱动器中调用 xls 文件。问题是我到目前为止的代码并不能防止应用程序在找不到文件的情况下崩溃。我需要某种错误处理来简单地防止这种情况发生并发送警报或其他东西。

这是我目前所拥有的

        var me = this;
    var fileName = "ReportCall.xls";
    var localFolder = Windows.Storage.ApplicationData.current.localFolder;
    var reportFile = localFolder.getFileAsync("Attachments\\Reports\\fileName");
    var file = local.Folder.getFileAsync(fileName);

    reportFile.done(function(file) {
    Windows.System.Launcher.launchFileAsync(file).done(
    function (success) {
    if (success) {
        // DO NOTHING
    } else {
        //Alert. File not found!
    }
    });
    });

有什么想法吗?

干杯

【问题讨论】:

  • 您的意思是在第一个 getFileAsync 调用中让参数为“Attachment\\Reports\\”+文件名,而不是使用“文件名”作为文件本身的名称?如所写,您的代码不是在寻找 ReportCall.xls。此外,您的下一行使用 local.Folder 而不是 localFolder,所以我看不出它是如何工作的——看起来像是一个错字。
  • 是的,你说得对。两者都是错别字。谢谢(你的)信息。知道如何使错误处理起作用吗?

标签: error-handling windows-8.1 winjs windows-applications


【解决方案1】:

用下面的代码解决了这个问题

var me = this;
var fileName = "ReportCall.xls";
var localFolder = Windows.Storage.ApplicationData.current.localFolder;
var file = localFolder.getFileAsync(fileName):

localFolder.getFileAsync("Attachments\\Reports + fileName").then(function)(file {
},function(e) {
    alert("file cannot be found");
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多