现在的位置: 首页 > 综合 > 正文

windows8 应用的激活与挂起

2012年04月11日 ⁄ 综合 ⁄ 共 5928字 ⁄ 字号 评论关闭
应用程序激活和悬架例子
这个演示了你如何处理活化、暂停、恢复和你的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 

抱歉!评论已关闭.