2

我想使用WinJS在Windows Metro中發生未處理的異常時,使用FlyOut導航到我的應用程序的主頁並顯示錯誤。問題是程序仍然繼續到base.js中的terminateAppHandler。使用WinJS的Windows Metro中的Gracefull異常處理

這是我走到這一步:

WinJS.Application.onerror = function (customEventObject) { 

    // Get the error message and name for this exception 
    var errorMessage = customEventObject.detail.error.message; 
    var errorName = customEventObject.detail.error.name; 

    // Bind them in an optionsObject to pass with the navigation 
    var optionsObject = { errName: errorName, errMsg: errorMessage }; 

    // Navigate home with information concerning the error 
    WinJS.Navigation.navigate("/pages/home/home.html", optionsObject); 

    // Need something to tell Windows that the error is "taken care" of 
    return false; // ?? 
} 

我會在這裏結束,終止應用程序:

var terminateAppHandler = function (data) { 
    debugger; 
    MSApp.terminateApp(data); 
}; 

任何建議或輸入更多的則是歡迎!

回答

4

返回true而不是false。

您還需要連接到window.onerror,以查找未通過application.onerror發送的異常。雖然如果發生異常,它確實是未處理的,並且沒有預測該應用程序處於什麼狀態。

+0

我可以在我的map.html中使用它,這是在iframe中打開的嗎? – sedran 2012-08-15 08:53:23

+1

僅當您在iframe中的代碼中運行WinJS應用程序對象(稱爲WinJS.Application.start())時。 IFrame是一個單獨的腳本上下文。 window.onerror處理也需要單獨完成。 – 2012-08-15 23:11:56

1

返回true應該完全按照您的需要進行操作。這就是我在我的應用程序中使用的。