javascript - Windows 应用商店应用程序 - WinJS : 0x800a1391 - JavaScript runtime error: 'Windows' is undefined

标签 javascript html windows-store-apps winjs

我正在尝试将 Google map 加载到 Windows 应用商店应用程序中。但是, native Windows RT 函数存在问题:Windows.UI.Popups.MessageDialog。我猜 Windows 命名空间超出了范围,但我现在无法弄清楚如何将此函数放入可使 Windows 命名空间可访问的范围内。感谢您的帮助。

编辑:我对此思考得越多,就越认为这与我正在加载 map.html 作为 iFrame 的源这一事实有关。所以 map.html 的上下文是一个 iFrame,而不是 Windows 应用商店应用程序页面。我猜 Windows 命名空间在 iFrame 中不可用?

来自 home.html:

            <iframe id="getLocationFrame" src="ms-appx-web:///pages/map/map.html" style="width:600px; height:600px;"></iframe>

异常(exception):

SCRIPT5009:ms-appx-web://76ad865e-25cf-485c-bc77-e18186bfd7ee/pages/map/map.js 中第 50 行第 17 列的未处理异常 0x800a1391 - JavaScript 运行时错误:“Windows”未定义 文件:map.js,行:50,列:17

map .html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script type="text/javascript" src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?  sensor=false"></script>        
    <script type="text/javascript" src="map.js"></script>

    <link href="/pages/map/css/map.css" rel="stylesheet" />
    <link href="//Microsoft.WinJS.1.0/css/ui-light.css" rel="stylesheet" />
</head>
<body>
    <p>Click to get your location.</p>
    <button id="getLocation">Get Location</button><br/>
    <div id="mapcontainer"></div><br />
    <small>
        <a id="anchorLargerMap" href="" style="color:#0000FF;text-align:left" target="_blank">View Larger Map</a>
    </small>
</body>
</html>

map .js:

(function () {
"use strict";

WinJS.UI.Pages.define("/pages/map/map.html", {

    // This function is called whenever a user navigates to this page. It
    // populates the page elements with the app's data.
    ready: function (element, options) {
        //Button "getLocation" event handler
        function getLocationClickHandler(eventInfo) {
            var myOptions = {
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var mapcontainer = document.getElementById("mapcontainer");
            var map = new google.maps.Map(mapcontainer, myOptions);

            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(locationSuccess, locationFail);
            }
        }

        var namespacePublicMembers = {
            locationSucessFunction: locationSuccess,
            locationFailFunction: locationFail,
            getLocationClickEventHandler: getLocationClickHandler
        };

        WinJS.Namespace.define("mapPage", namespacePublicMembers);
        var getLocationButton = document.getElementById("getLocation");
        getLocationButton.addEventListener("click", getLocationClickHandler, false);

        function locationSuccess(position) {
            var initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(initialLocation);
            var marker = new google.maps.Marker({
                position: initialLocation,
                map: map,
                title: "You are here."
            });

            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var url = "http://maps.google.com/maps?q=" + latitude + "," + longitude + "&zoom=13&markers=" + latitude + "," + longitude;
            $("#anchorLargerMap").attr('href', url);
        }

        function locationFail() {
            var md = new Windows.UI.Popups.MessageDialog("Could not find you!", "").showAsync;  -- ********* THIS LINE THROWS EXCEPTION *********
        }
    }
});
})();

最佳答案

在 Web 隔间中执行的代码 - 您的 URL 说明代码所在的位置 - 无法访问 WinRT 组件。您需要使用 postMessage 等在两个安全上下文之间进行通信。

关于javascript - Windows 应用商店应用程序 - WinJS : 0x800a1391 - JavaScript runtime error: 'Windows' is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14024526/

相关文章:

javascript - 鼠标悬停功能不起作用

javascript - - - - 正在转换为 — 同时传递给 href

javascript - 使用 jquery 更改 HTML 中标签内的文本值

c# - 如何访问FlipView内DataTemplate内的xaml控件

c# - Win8 ComboBox IsDropDownOpen + 可见性导致 UI 出现故障

javascript - 为什么我的可搜索过滤列表不起作用?

Javascript搜索并显示具有匹配关键字的div

JavaScript 提交错误的表单

html - Bootstrap 3 在加载时忽略 custom.css

javascript - UWP:无法访问许可证信息