我不知道如何使我的winjs(react-winjs)应用程序与硬件后退按钮一起使用。这是我的导航功能的一个示例:

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

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


console.log(WinJS.Navigation.history)以正确的历史顺序输出称为“ backStack”的正确数组,但是单击Windows Phone Emulator上的硬件后退按钮只是退出应用程序。

我是否缺少明显的东西?

这是我设法找到并尝试的方法,但没有成功(我也确实为C#找到了一些好的文档,但是我不需要的是):

link 1

link 2

谢谢

最佳答案

确实,这是一个非常愚蠢的错误,我在不等待winjs / windows准备就绪的情况下初始化了我的应用程序,这就是我应该如何初始化它的方式:

(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();
})();


这样,我可以在组件componentWillMount函数中将eventListener添加到“ backclick”,并且可以正常工作:

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

关于javascript - 处理硬件后退按钮单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34014986/

10-16 21:06