2013-09-23 45 views
0

我想從我的項目中調用C#的JavaScript,我想調用一個我已經用參數定義的javascript函數,但我堅持要如何去做如何從C#注入JavaScript?

MainPage.xaml有這個我的:CordovaView,它包裝了我的phonegap項目,但是我對如何「注入」並在其中運行javascript有點遺憾。

有沒有人這樣做?我試圖做推消息

回答

1

我沒有這樣做與科爾多瓦本身,但一般來說,你只需要記住代碼執行的地方。 C#代碼正在服務器上執行,並且JavaScript正在客戶端上執行,因此,您希望C#發出可供客戶端使用的值。

以下JavaScript片段基於Razor引擎語法,希望它足以讓您開始。

function alertVar(someVal) { 
    alert("JS, someVal = " + someVal); 
} 

$(document).ready(function() { 
    // in this case the server interpolates @Model and returns HTML with the value replaced 
    // wrap it in quotes so the js engine treats that value as a string 
    // this method will then fire when the document loads 
    alertVar("@Model.ServerValueForClient"); 
}); 

這是該服務器將插補處理後發送給客戶端的HTML(假設某處的服務器上設置的值@Model.ServerValueForClient = "set by the server";

$(document).ready(function() { 
    // in this case the server interpolates @Model and returns HTML with the value replaced 
    // wrap it in quotes so the js engine treats that value as a string 
    // this method will then fire when the document loads 
    alertVar("set by the server"); 
}); 

HTH