【问题标题】:How to make a loading animation for windows8 app如何为 windows8 应用程序制作加载动画
【发布时间】:2013-02-16 01:03:54
【问题描述】:

我有一个通过 javascript、css 和 html 制作的 windows8 应用程序,它显示一个 rss 提要。 有时,加载此提要可能需要 2-10 秒,这对我来说不是问题,但对于需要该应用程序的人来说,他们想知道它正在加载而不是冻结。 我有一个动画加载动画 (GIF),但我不知道如何在 rss 加载它时显示它。 有任何想法吗?感谢您的宝贵时间。

这是主要的javascript,

(function () {
"use strict";

  WinJS.Binding.optimizeBindingReferences = true;

  var app = WinJS.Application;
  var activation = Windows.ApplicationModel.Activation;
  var articlesList;

  app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.launch) {
        if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
            // TODO: This application has been newly launched. Initialize
            // your application here.
        } else {
            // TODO: This application has been reactivated from suspension.
            // Restore application state here.
        }

        articlesList = new WinJS.Binding.List();
        var publicMembers = { ItemList: articlesList };
        WinJS.Namespace.define("C9Data", publicMembers);

        args.setPromise(WinJS.UI.processAll().then(downloadC9BlogFeed));
      }
  };

  function downloadC9BlogFeed() {
    WinJS.xhr({ url: "http://channel9.msdn.com/coding4fun/articles/RSS" }).then(function (rss) {  // this is where it is calling the RSS
      var items = rss.responseXML.querySelectorAll("item");

      for (var n = 0; n < items.length; n++) {
        var article = {};
        article.title = items[n].querySelector("title").textContent;
        var thumbs = items[n].querySelectorAll("thumbnail");
        if (thumbs.length > 1) {
          article.thumbnail = thumbs[1].attributes.getNamedItem("url").textContent;
          article.content = items[n].textContent;
          articlesList.push(article);
        }
      }
    });
  }

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

【问题讨论】:

  • 你是如何加载你的提要的?假设您正在执行某种异步请求,您可以在发起请求时显示您的 GIF,并在响应返回时将其隐藏在回调中。
  • 这是主要的javascript,

标签: javascript animation windows-8 loading


【解决方案1】:

@mleroy 已经描述了加载它的正确方法。伪代码(CoffeeScript):

downloadC9BlogFeed = () ->
  listView.style.display = 'none'
  placeholderAnimation.style.display = 'block'
  WinJS.xhr(url)
  .then (rss) ->
    items = rss.responseXML.querySelectorAll("item")
    items.forEach (item) ->
       [process the item without having to use items[n] multiple times]
  .then () ->
    placeholderAnimation.style.display = 'none'
    listView.style.display = 'block'

【讨论】:

  • 对不起,我真的不擅长javascript和coffeescript。它在哪里说[您在此处处理,我会将脚本放入以加载 rss 吗?
  • 我稍微更新了示例代码。第一个延续(“then”)和第二个延续之间的块基本上是您已经拥有的相同代码,即整个“function(rss){...}”
  • 所以如果我将该代码直接粘贴到 javascript 文件中,它应该可以工作吗?还需要对 placeholderAnimation 做任何事情吗?例如使其成为 div 或 css 规则?...也感谢您到目前为止的帮助 :)
  • 是的,placeholderAnimation 是一个 id="placeholderAnimation" 的 div,您可以在其中放置要在加载提要时显示的动画或内容。当然,您需要将我的伪代码转换为真正的 JavaScript,但我总是懒得在示例代码中编写所有这些“function() {}”和“var foo”噪音。
猜你喜欢
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多