windows8 应用的激活与挂起

应用程序激活和悬架例子
这个演示了你如何处理活化、暂停、恢复和你的Metro风格应用程序。
复制代码
(function () {
    var order = 0;
    var app = WinJS.Application;
    var myfile = "myfile.text";
    sdkSample.scenarioSelection = false;

    function onScenarioChanged() {
        // Do any necessary clean up on the output, the scenario id
        // can be obtained from sdkSample.scenarioId.
        sdkSample.displayStatus("");

        if (sdkSample.scenarioId === 1) {
            document.getElementById('scenario1OutputTextArea').style.display = 'block';
        }
        if (sdkSample.scenarioId === 2) {
            document.getElementById('scenario1OutputTextArea').style.display = 'none';
        }
    }

    function domcontentloadedHandler() {
        document.getElementById("scenarios").addEventListener("change", /*@static_cast(EventListener)*/onScenarioChanged, false);

        order += 1;
        document.getElementById("scenario1Output").innerHTML += "DOMContentLoaded was triggered at order: " + /*@static_cast(String)*/order + ".";
    }

    function loadHandler() {
        order += 1;
        document.getElementById("scenario1Output").innerHTML += "<br/><br/>load was triggered at order: " + /*@static_cast(String)*/order + ".";
    }

    function activatedHandler(eventArgs) {
        order += 1;
        document.getElementById("scenario1Output").innerHTML += "<br/><br/>activated was triggered at order: " + /*@static_cast(String)*/order + ". ";

        if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
            sdkSample.selectScenario("1");
            document.getElementById('scenario1OutputTextArea').style.display = 'block';

            // Check if the activatedReason means that this is a re-activation after a suspension followed by a graceful
            // termination. If it is then we must have saved the state in suspending handler. Retrieve the persisted state.
            var activatedReason = eventArgs.previousExecutionState;
            if (activatedReason === Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {

                // If there is going to be some asynchronous operation done during activation then
                // the app can signal the need to handle activation of the application asynchronously.
                // To do so the app can use the getDeferral method.
                var deferral = eventArgs.activatedOperation.getDeferral();

                // Populate the text box with the previously saved value
                app.local.readText(myfile, "default").then(function (str) {
                    document.getElementById("userText").value = str;

                    // After the asynchronous operation is done the app must call complete on the deferral object
                    // as follows else the app would get terminated.
                    deferral.complete();
                });
            }
        }

        if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {
            sdkSample.selectScenario("2");
            document.getElementById('scenario1OutputTextArea').style.display = 'none';

            // This is a protocol activation.
            // Protocol format currently supported in this sample is: sdksampleprotocol:domain?src=[some url]
            document.getElementById("scenario2Output").innerHTML += "This is Protocol activation.";
            document.getElementById("scenario2Output").innerHTML += "<br />Protocol format used for this activation: " +
                eventArgs.uri.rawUri + "<br/>";
        }

    //    if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.search) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.sendTarget) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.filePicker) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.contactPicker) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.device) {
    //        // noop
    //    } else if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.printTaskSettings) {
    //        // noop
    //    } else { // if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.cameraSettings)
    //        // noop
    //    }
    }

    function suspendingHandler(eventArgs) {
        // If there is going to be some asynchronous operation done during suspension then
        // the app can signal the need to handle suspension of the application asynchronously.
        // To do so the app can use the getDeferral method.
        var deferral = eventArgs.suspendingOperation.getDeferral();

        // This is only for advanced scenarios when using a file is necessary to persist data.
        app.local.writeText(myfile, document.getElementById("userText").value).then(function () {
            // After the asynchronous operation is done the app must call complete on the deferral object
            // as follows else the app would get terminated.
            deferral.complete();
        });
    }

    function resumingHandler() {
    }

    document.addEventListener("DOMContentLoaded", domcontentloadedHandler, false);
    window.addEventListener("load", /*@static_cast(EventListener)*/loadHandler, false);
    Windows.UI.WebUI.WebUIApplication.addEventListener("activated", activatedHandler, false);
    Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", suspendingHandler, false);
    Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", resumingHandler, false);
复制代码

})();

 

完整示例

/Files/risk/windows8/应用激活sample.rar 

posted @   西瓜小强  阅读(463)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示