zoukankan      html  css  js  c++  java
  • 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 

    作者:risk
    出处:http://www.cnblogs.com/risk
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Android 四大组件学习之ContentProvider三
    JavaScript遍历table
    codecombat之KithGard地牢19-37关代码分享
    【学习笔记】信息系统项目管理-项目採购管理-合同分类
    【记中关村.西北食府.兰州拉面】诗一首
    HDU 1042.N!【高精度乘法】【8月24】
    Mac安装MySQL
    Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III
    UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_
    ftk学习记(combox篇)
  • 原文地址:https://www.cnblogs.com/risk/p/2494709.html
Copyright © 2011-2022 走看看