下面是对使用JavaScript的Bing Maps Trip Optimizer示例的介绍,讲述应用的JavaScript部分组织形式,本地的上下文和网络上下文之间的通信。

WinRT需要访问网络的代码,下面是一些需要访问网络的活动示例。

q 加载一个外部JavaScript文件,比如Bing Maps的AJAX控件。

q 加载外部CSS。

q 使用XmlHttpRequest。

Bing Maps Trip Optimizer将界面分为两个HTML文件,即“default.html”和“mapControl.html”,“default.html”属于local context(本地上下文),其可以引用WinRT(这个包括自定义C++的WinRT组件,不过不能访问网络),“default.html”表示应用的主页面。“mapControl.html”可称为web context(网络上下文),因为其可访问网络,但不能访问WinRT。“mapControl.html”是一个local context的内嵌框架。下图是local context和web context间的关系。

开发 Windows 8 Bing地图应用(2)

注意:就安全性而言,在一个单独的模块中维护web代码,同样会增加应用的安全性。可能的话有必要验证从web context接收到local context的内容,这样可以减少发生安全性缺陷的可能,比如缓冲区溢出。

接下来在local context中引用web context。local context(default.html)中body标签定义一个内嵌框架(iframe标签)来放置web context,代码如下。

<body onload="OnLoad();" style="padding: 0; margin: 0; position: absolute; left: 0px;

width: 100%; top: 0px; height: 100%; overflow: hidden">

<iframe : parallel

};

window.parent.postMessage(JSON.stringify(message), '*');

}

从web context,而不是直接从local context初始化WinRT组件的加载,这样确保web context和local context都在创建WinRT组件前被加载。

相关文章:

  • 2021-09-17
  • 2021-10-13
  • 2021-10-05
  • 2021-09-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
相关资源
相似解决方案