2015-12-01 103 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... 
    }); 
}