2012-05-03 44 views
3

嗨Iam的Windows手機新鮮。誰能告訴我如何在JavaScript中調用C#方法Phonegap(Windows Mobile)。謝謝。如何在JavaScript中調用C#代碼在phonegap windows手機

Iam得到以下錯誤。

錯誤:「Unable to locate command :: MyCommand」。

這是我的代碼文件的背後

using System; 
using System.Collections.Generic; 
using System.Linq; using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using System.IO; 
using System.Windows.Media.Imaging; 
using System.Windows.Resources; 
using WP7CordovaClassLib.Cordova.Commands; 
namespace CSharpToJS 
{ 
public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void GapBrowser_Loaded(object sender, RoutedEventArgs e) 
    { 

    } 

} 
namespace WP7GapClassLib.PhoneGap.Commands 
{ 
    public class MyCommand : BaseCommand 
    { 
     public void DoThis(string args) 
     { 
      // TODO: 
     } 
    } 
} 
} 

這裏是我的index.html文件:

<!DOCTYPE html> 
<html> 
<head> 
<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>Cordova WP7</title> 
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8" /> 
<script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script> 
<script type="text/javascript"> 
    document.addEventListener("deviceready", onDeviceReady, false); 
    // once the device ready event fires, you can safely do your thing! -jm 
    function onDeviceReady() { 
     document.getElementById("welcomeMsg").innerHTML += "Cordova is ready! version=" + window.device.cordova; 
     console.log("onDeviceReady. You should see this message in Visual Studio's output window."); 
     var args = { "message": "whatever" }; 
     Cordova.exec(null, null, "MyCommand", "DoThis", args); 
    } 
    </script> 
</head> 
    <body> 
<h1> 
    Hello Cordova</h1> 
<div id="welcomeMsg"> 
</div> 
</body> 
</html> 
+2

不要把Java與Javascript混淆:) –

回答

1

你應該能夠這樣使用plugin architecturePhoneGap.exec()方法來完成。

創建從BaseCommand繼承C#類(你也必須使用此特定的命名空間):

namespace WP7GapClassLib.PhoneGap.Commands 
{ 
    public class MyCommand : BaseCommand 
    { 
     public void DoThis(string args) 
     { 
     // TODO: 
     } 
    } 
} 

調用此方法從Javascript:

var args = {"message":"whatever"};  
PhoneGap.exec(null, null, "MyCommand", "DoThis", args); 
+0

嗨Johansson,謝謝你,我嘗試過使用上面的代碼,但它給出的錯誤如「phonegap is undefined」,我應該做什麼修改? –

+0

Iam通過嘗試這段代碼得到錯誤,可以用上面的代碼解釋我嗎?我有錯誤嗎?在此先感謝 –

+0

您是否使用了確切的名稱空間? –

相關問題