Cordova PhoneGap应用程序在windowsphone中不工作?

Cordova PhoneGap应用程序在windowsphone中不工作?,cordova,windows-phone-7,Cordova,Windows Phone 7,我正在将Visual Studio 2010与Windows Phone SDK和Phonegap1.4.1一起使用。我创建了一个phonegapStarter项目。在index.html中,我编写了加速器示例代码。 音差 //提供我们自己的控制台,如果它不存在,巨大的开发援助! if(typeof window.console==“未定义”){ window.console={log:function(str){window.external.Notify(str);}; } //将任何错

我正在将Visual Studio 2010与Windows Phone SDK和Phonegap1.4.1一起使用。我创建了一个phonegapStarter项目。在index.html中,我编写了加速器示例代码。


音差
//提供我们自己的控制台,如果它不存在,巨大的开发援助!
if(typeof window.console==“未定义”){
window.console={log:function(str){window.external.Notify(str);};
}
//将任何错误输出到上面创建的控制台日志。
window.onerror=函数(e){
log(“window.onerror::”+JSON.stringify(e));
};
log(“已安装的控制台!”);
var-devicerady=false;
函数轮号(num){
var-dec=3;
var result=Math.round(num*Math.pow(10,12月))/Math.pow(10,12月);
返回结果;
}
//-------------------------------------------------------------------------
//加速
//-------------------------------------------------------------------------
var watchAccelId=null;
/**
*开始观察加速
*/
var watchAccel=函数(){
log(“watchAccel()”);
//成功回调
var成功=功能(a){
document.getElementById('x').innerHTML=roundNumber(a.x);
document.getElementById('y').innerHTML=roundNumber(a.y);
document.getElementById('z').innerHTML=roundNumber(a.z);
log(“watchAccel成功回调”);
};
//失败回调
var失效=功能(e){
日志(“watchAccel失败回调,错误代码为”+e);
stopAccel();
setAccelStatus(加速度计错误消息[e]);
};
//每1秒更新一次加速度
var opt={};
选择频率=50;
WatchAccelerID=navigator.Accelerator.watchAcceleration(成功、失败、选择);
setAccelStatus(“运行”);
};
/**
*不要再看加速度了
*/
var stopAccel=函数(){
log(“stopAccel()”);
setAccelStatus(“已停止”);
if(watchAccelId){
navigator.Accelerator.clearWatch(WatchAccelerID);
watchAccelId=null;
}
};
/**
*获得当前加速度
*/
var getAccel=函数(){
log(“getAccel()”);
//如果正在运行,请停止加速
//stopAccel();
//成功回调
var成功=功能(a){
document.getElementById('x').innerHTML=roundNumber(a.x);
document.getElementById('y').innerHTML=roundNumber(a.y);
document.getElementById('z').innerHTML=roundNumber(a.z);
};
//失败回调
var失效=功能(e){
log(“getAccel失败回调,错误代码为“+e”);
setAccelStatus(加速度计错误消息[e]);
};
//打电话
var opt={};
navigator.Accelerator.getCurrentAcceleration(成功、失败、选择);
};
/**
*设置加速计状态
*/
var setAccelStatus=功能(状态){
document.getElementById('accel_status')。innerHTML=status;
};
/**
*当页面完成加载时调用的函数。
*/
函数init(){
文档.添加的事件列表器(“deviceready”,函数(){
DeviceRady=真;
console.log(“Device=“+Device.platform+”“+Device.version”);
},假);
setTimeout(函数(){
如果(!DeviceRady){
警报(“错误:PhoneGap未初始化。演示将无法正确运行。”);
}
}, 1000);
}

html部分是

加速
停止
X:
Y:
Z:
行动


但我没有收到任何输出。单击按钮时,我看不到X、Y、Z标签有任何更改。原因可能是什么?

这并没有回答问题,但我注意到您的代码片段中有一点是使用了alert()。这些不适用于Windows Phone,您必须使用PhoneGap通知API,例如navigator.Notification.alert()。有关该方法采用的参数的详细信息,请参阅。
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/> 
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen"/>

    <script type="text/javascript">
        // provide our own console if it does not exist, huge dev aid!
        if (typeof window.console == "undefined") {
            window.console = { log: function (str) { window.external.Notify(str); } };
        }

        // output any errors to console log, created above.
        window.onerror = function (e) {
            console.log("window.onerror ::" + JSON.stringify(e));
        };

        console.log("Installed console ! ");
</script>


<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>      


<script type="text/javascript" charset="utf-8">

var deviceReady = false;

function roundNumber(num) {
    var dec = 3;
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

//-------------------------------------------------------------------------
// Acceleration
//-------------------------------------------------------------------------
var watchAccelId = null;

/**
* Start watching acceleration
*/
var watchAccel = function () {
    console.log("watchAccel()");

    // Success callback
    var success = function (a) {
        document.getElementById('x').innerHTML = roundNumber(a.x);
        document.getElementById('y').innerHTML = roundNumber(a.y);
        document.getElementById('z').innerHTML = roundNumber(a.z);
        console.log("watchAccel success callback");
    };

    // Fail callback
    var fail = function (e) {
        console.log("watchAccel fail callback with error code " + e);
        stopAccel();
        setAccelStatus(Accelerometer.ERROR_MSG[e]);
    };

    // Update acceleration every 1 sec
    var opt = {};
    opt.frequency = 50;
    watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);

    setAccelStatus("Running");
};

/**
* Stop watching the acceleration
*/
var stopAccel = function () {
    console.log("stopAccel()");
    setAccelStatus("Stopped");
    if (watchAccelId) {
        navigator.accelerometer.clearWatch(watchAccelId);
        watchAccelId = null;
    }
};

/**
* Get current acceleration
*/
var getAccel = function () {
    console.log("getAccel()");

    // Stop accel if running
    //stopAccel();

    // Success callback
    var success = function (a) {
        document.getElementById('x').innerHTML = roundNumber(a.x);
        document.getElementById('y').innerHTML = roundNumber(a.y);
        document.getElementById('z').innerHTML = roundNumber(a.z);
    };

    // Fail callback
    var fail = function (e) {
        console.log("getAccel fail callback with error code " + e);
        setAccelStatus(Accelerometer.ERROR_MSG[e]);
    };

    // Make call
    var opt = {};
    navigator.accelerometer.getCurrentAcceleration(success, fail, opt);
};

/**
* Set accelerometer status
*/
var setAccelStatus = function (status) {

    document.getElementById('accel_status').innerHTML = status;
};

/**
* Function called when page has finished loading.
*/
function init() {

    document.addEventListener("deviceready", function () {
        deviceReady = true;
        console.log("Device=" + device.platform + " " + device.version);
    }, false);
    window.setTimeout(function () {
        if (!deviceReady) {
            alert("Error: PhoneGap did not initialize.  Demo will not run correctly.");
        }
    }, 1000);
}
<h1>Acceleration</h1>
<div id="info">
    <div id="accel_status">Stopped</div>
    <div><table width="100%">
        <tr><td>X:</td><td id="x">&nbsp;</td></tr>
        <tr><td>Y:</td><td id="y">&nbsp;</td></tr>
        <tr><td>Z:</td><td id="z">&nbsp;</td></tr>
    </table></div>
</div>



<h2>Action</h2>
<a href="#" class="btn large" onclick="getAccel();">Get Acceleration</a>
<a href="#" class="btn large" onclick="watchAccel();">Start Watch</a>
<a href="#" class="btn large" onclick="stopAccel();">Clear Watch</a>
<h2>&nbsp;</h2><a href="index.html" class="backBtn">Back</a>