2013-01-08 22 views
0

我試圖在有人試圖關閉未保存的文件時在我的Windows RT應用程序中啓動保存對話框。但是,我不斷收到一個0x80070005 - JavaScript runtime error: Access is denied錯誤使用Javascript的Windows Store應用程序中的訪問被拒絕

這是我使用啓動消息對話框的代碼。當選擇「不保存」(並且運行BlankFile())時,一切都運行正常。然而,當你選擇「保存文件」它拋出的訪問被拒絕錯誤時嘗試運行.pickSaveFileAsync()

function createNewFile() 
{ 
    if (editedSinceSave) 
    { 
     // Create the message dialog and set its content 
     var msg = new Windows.UI.Popups.MessageDialog("Save this file?", 
      "Save Changes"); 

     // Add commands 
     msg.commands.append(new Windows.UI.Popups.UICommand("Don't Save", 
      function (command) { 
       BlankFile(); 
      })); 

     msg.commands.append(new Windows.UI.Popups.UICommand("Save File", 
      function (command) { 
       //saveFile(true, true); 
       testPop("test"); 
      })); 

     // Set the command that will be invoked by default 
     msg.defaultCommandIndex = 2; 

     // Show the message dialog 
     msg.showAsync(); 
    } 
} 

function testPop(text) { 
    var msg = new Windows.UI.Popups.MessageDialog(text, ""); 
    msg.showAsync(); 
} 

回答

1

你的核心問題是你正在綁定顯示另一個消息對話框ontop。我討論的細節和解決方案在這裏: What is the alternative to `alert` in metro apps?

然而,你自然需要這是發生 - 我建議看着建立一個不同類型的流而不是堆疊對話框。

+0

啊!我明白你對這個流程的看法,但這裏有什麼選擇?如果有人試圖關閉文件而不保存,則標準過程是提示他們保存並顯示文件選擇器對話框。我沒有看到圍繞此流程的解決方法 – roryok

+0

您可以通過將WinJS.promise.timeout延遲推送到選取器來解決此問題。 –

+0

看起來好像showAsync的功能更好。我想我會像 – roryok

0

的辦法解決,這似乎是設置命令ID,趕上它的showAsync()done()功能,如所以

function createNewFile() 
{ 
    if (editedSinceSave) 
    { 
     // Add commands and set their CommandIds 
     msg.commands.append(new Windows.UI.Popups.UICommand("Dont Save", null, 1)); 
     msg.commands.append(new Windows.UI.Popups.UICommand("Save File", null, 2)); 

     // Set the command that will be invoked by default 
     msg.defaultCommandIndex = 1; 

     // Show the message dialog 
     msg.showAsync().done(function (command) { 
      if (command) { 
       if (command.id == 1){ 
        BlankFile(); 
       } 
       else { 
        saveFile(true, true); 
       } 
      } 
     }); 
    } 
} 

這不會引發任何錯誤。我不知道爲什麼用另一種方式拋出錯誤,因爲它看起來沒有什麼不同!

+0

當然,它是不同的。在您的原始代碼中,您嘗試通過向保存命令添加一個函數來在現有代碼上創建一個對話框,這在WinJS應用程序中是不允許的。 在您修改後的版本中。你在鉤住「完成」延續方法。所以現在,在關閉彈出窗口後(通過點擊「保存文件」按鈕),你的「保存文件」代碼將被執行。 順便說一下,從邏輯上講,這也是正確的做法。一旦用戶點擊了保存按鈕,就不再需要該對話框了。 – guyarad

相關問題