2015-12-01 101 views
1

我無法弄清楚如何使我的winjs(react-winjs)應用程序與硬件後退按鈕一起使用。這是我的導航功能的一個例子:處理硬件後退按鈕單擊

handleNavigation(location) { 
    this.setState({ 
     location: location 
    }); 

    WinJS.Navigation.navigate(`/${location}`); 
    console.log(WinJS.Navigation.history); 
} 

console.log(WinJS.Navigation.history)輸出正確的陣列稱爲「堆棧中」以正確的歷史訂單,但點擊硬件返回鍵在Windows Phone模擬器只是簡單的退出程序。

我錯過了一些明顯的東西嗎?

這是我設法找到和嘗試,但沒有成功(我沒找到C#一些好的文檔爲好,但什麼是不是我所需要):

link 1

link 2

感謝

+1

其中顯示的代碼,您連接最多此事件處理程序? – Claies

+0

另外,您是否嘗試從第一條路線返回? –

+0

有「鏈接1」和「鏈接3」,忘記添加「鏈接2」 – Lars

回答

1

事實上,這是一個非常愚蠢的錯誤,我初始化我的應用程序,而無需等待winjs /窗口做好準備,這是我應該initilized它:

(function() { 
"use strict"; 

var app = WinJS.Application; 
var activation = Windows.ApplicationModel.Activation; 

app.onactivated = function (args) { 
    if (args.detail.kind === activation.ActivationKind.launch) { 
     if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) { 
      // This is how you should initialize your app 
      ReactDOM.render(<App />, document.getElementById('app')); 
     } else { 
      // TODO: This application was suspended and then terminated. 
      // To create a smooth user experience, restore application state here so that it looks like the app never stopped running. 
     } 
     args.setPromise(WinJS.UI.processAll()); 
    } 
}; 

app.oncheckpoint = function (args) { 
    // TODO: This application is about to be suspended. Save any state that needs to persist across suspensions here. 
    // You might use the WinJS.Application.sessionState object, which is automatically saved and restored across suspension. 
    // If you need to complete an asynchronous operation before your application is suspended, call args.setPromise(). 
}; 

app.start(); 
})(); 

這樣我可以給的addEventListener「backclick」在我的組件componentWillMount功能,它的工作原理:

componentWillMount() { 
    WinJS.Application.addEventListener("backclick", function() { 
     document.body.style.background = "red"; //or for example setState/show notification etc... 
    }); 
}