2016-09-11 4 views
1

HTML/JS/CSSで構築された単純なUWPアプリケーションがあります。それは本質的にウェブサイトであり、何らかのデータをwebviewにロードするなど、普通のものではありません。Windows UWPアプリケーションライフサイクル:JS/HTML/CSSアプリケーションでのリロードを防止する方法

私が苦労しているのは、アプリケーションのライフサイクルとその「再開」状態です。アプリケーションはすでに実行されています。ユーザーはアプリケーションを再び開き、アプリケーション全体が再描画され、すべてのhtml + jが取得されます。基本的にリロードを引き起こしている(F5経由のように)再度実行してください。どうすればそれを防ぐことができますか?

ここには非常に基本的なmain.jsファイルがあり、前回の実行状態とアプリケーション実行状態 "実行中"を比較する行に注意してください。私は、私のアプリのリロードを防ぐために(つまり、言い換えれば再レンダリングするために)何をすべきかと思うので、初期化プロセスをやり直すことはありません。すべてのリソースが再びロードされるので、ちょうど悪く見えます。

(function() { 
    "use strict"; 

    var app = WinJS.Application; 
    var activation = Windows.ApplicationModel.Activation; 
    var isFirstActivation = true; 

    app.onactivated = function (args) { 
     if (args.detail.kind === activation.ActivationKind.voiceCommand) { 
      // TODO: Handle relevant ActivationKinds. For example, if your app can be started by voice commands, 
      // this is a good place to decide whether to populate an input field or choose a different initial view. 
     } 
     else if (args.detail.kind === activation.ActivationKind.launch) { 
      // A Launch activation happens when the user launches your app via the tile 
      // or invokes a toast notification by clicking or tapping on the body. 
      if (args.detail.arguments) { 
       // TODO: If the app supports toasts, use this value from the toast payload to determine where in the app 
       // to take the user in response to them invoking a toast notification. 
      } 
      else if (args.detail.previousExecutionState === activation.ApplicationExecutionState.terminated) { 
       // TODO: This application had been suspended and was then terminated to reclaim memory. 
       // To create a smooth user experience, restore application state here so that it looks like the app never stopped running. 
       // Note: You may want to record the time when the app was last suspended and only restore state if they've returned after a short period. 
      } 
      else if (args.detail.previousExecutionState === activation.ApplicationExecutionState.running) { 
       isFirstActivation = false; 
      } 
     } 

     if (!args.detail.prelaunchActivated) { 
      // TODO: If prelaunchActivated were true, it would mean the app was prelaunched in the background as an optimization. 
      // In that case it would be suspended shortly thereafter. 
      // Any long-running operations (like expensive network or disk I/O) or changes to user state which occur at launch 
      // should be done here (to avoid doing them in the prelaunch case). 
      // Alternatively, this work can be done in a resume or visibilitychanged handler. 
     } 

     if (isFirstActivation) { 
      // TODO: The app was activated and had not been running. Do general startup initialization here. 
      document.addEventListener("visibilitychange", onVisibilityChanged); 
      args.setPromise(WinJS.UI.processAll().then(function(){ WinJS.UI.enableAnimations();})); 
     } 

     isFirstActivation = false; 
    }; 

    function onVisibilityChanged(args) { 
     if (!document.hidden) { 
      // TODO: The app just became visible. This may be a good time to refresh the view. 
     } 
    } 

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

})(); 
+0

ここで唯一の選択肢は、webviewでjs/html/cssアプリをラップしてXAMLアプリをビルドすることです。別のコンテンツをiframe/webviewにロードしています... – belzebu

+0

アプリのリロードを防ぐことはできませんアプリが単一ページアプリケーションの場合は、イベントを一時停止するとその状態を保存し、再開するときは前のページに移動することができます。 –

+0

違いはマニフェストの「スタートページ」でした。私の返事を見てください。 – belzebu

答えて

1

オクラホマので、デバッグのいくつかの時間後に、アプリケーションのフロントを書き換えると背中、私はここで犯人がアプリケーションマニフェストで「スタートページ」であったことに気づきました。

以前の実行状態であってもアプリケーションが再ロードされるという問題は、ユーザーがアプリケーションを再オープンするたびに "app.Start()"がトリガーされることによって発生しました。しかし、UWPサンプルキットのサンプルはこの動作に苦しみませんでした。アプリケーションが実際に起動されたときに、「app.Start()」が1回だけトリガーされていることに気付きました。それ以降の各オープニングは、私のアプリケーションのように(app.Start()に)もはやその点に到達しませんでした。

以前は、Content Security Policy問題のため、StartPage = "ms-appx-web:///index.html"を設定するこの値がありました。しかし、この値をサンプルと比較すると、彼らは "StartPage = index.html"を使用して、私が問題であると思うようになり、私は正しくなりました。コンテンツセキュリティポリシーに関連するエラー(インラインスクリプトが置き換えられる)がないことを確認するには数時間かかりましたが、最終的には機能しました。

「StartPage = 'ms-appx-web:///index.html'」は、「StartPage = 'index.html'」に対して、アプリケーションが毎回再起動される原因を説明できませんその状態を正しく維持していますが、私の問題を解決しました。

関連する問題