如何在不使用 WinjS 库的情况下在通用 Windows 应用程序中添加后退按钮事件? [英] How to add back button event in Universal Windows App without using WinjS Library?

查看:23
本文介绍了如何在不使用 WinjS 库的情况下在通用 Windows 应用程序中添加后退按钮事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 main.js

This is my main.js

(function () {
"use strict";

//No need of WinJS
var activation = Windows.ApplicationModel.Activation;
var roaming = Windows.Storage.ApplicationData.current.roamingSettings;

// For App Start Up
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
    if (args.detail[0].kind === activation.ActivationKind.launch) {
        if (roaming.values["currentUri"]) {
            if (roaming.values["UserName"])
            {
                localStorage.setItem("UserName", roaming.values["UserName"]);
                window.location.href = roaming.values["currentUri"];
            }
        }
    }
});

// For App Suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
    roaming.values["currentUri"] = window.location.href;
    roaming.values["UserName"] = localStorage.getItem("UserName");
});

// For Resuming App
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
    var roam = Windows.Storage.ApplicationData.current.roamingSettings;
    if (roam) {
        if (roam.values["currentUri"]) {
            localStorage.setItem("UserName", roam.values["UserName"]);
            window.location.href = roam.values["currentUri"];
        }
    }
}, false);
// not working backpressed event
Windows.UI.WebUI.WebUIApplication.addEventListener("backpressed", function (args) {
   // to do
}, false);})();

我需要在不使用 winjs 库的情况下为 Windows 手机添加返回按键事件吗?有人可以推荐我吗?

I need to add back key press event for windows phone without using winjs library? Can anyone suggest me?

我在我的应用程序中使用 ms-appx-web 上下文.我不想使用 winjs 库.

I am using ms-appx-web context in my app. I dont want to use winjs library.

推荐答案

我需要在不使用 winjs 库的情况下为 Windows 手机添加返回按键事件吗?

I need to add back key press event for windows phone without using winjs library?

backpressed 事件应该附加到 Windows.Phone.UI.Input.HardwareButtons 而不是 Windows.UI.WebUI.WebUIApplication.

The backpressed event should be attached to Windows.Phone.UI.Input.HardwareButtons but not Windows.UI.WebUI.WebUIApplication.

如果您参考 HardwareButtons.BackPressedHardwareButtons,你会发现 backpressed 事件是这样使用的:

If you refer to HardwareButtons.BackPressed and HardwareButtons, you will find the backpressed event is used like this:

var hardwareButtons = Windows.Phone.UI.Input.HardwareButtons;
function onBackPressed(eventArgs) { /* Your code */ }

// addEventListener syntax
hardwareButtons.addEventListener("backpressed", onBackPressed);
hardwareButtons.removeEventListener("backpressed", onBackPressed);

而且由于您不是在制作单页应用程序.此事件应附加在每个新页面的 JS 代码中.

And since you are not making a Single Page Application. This event should be attached on every new page's JS codes.

更新:如果您想以编程方式了解您当前的设备,可以使用以下 if 语句:

Update: If you want to know your current device programmatically, you can use the following if-statement:

if (deviceInfo.operatingSystem.toLowerCase() == "windowsphone")
{
     //do your windows phone logic
} else if (deviceInfo.operatingSystem.toLowerCase() == "windows")
{
     //do your windows logic
}

这篇关于如何在不使用 WinjS 库的情况下在通用 Windows 应用程序中添加后退按钮事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆