积极答复者
如何从托管代码中调用前台JS方法后,得到此方法返回的强类型值?

问题
-
在前台HTML中,有一个JS方法,它返回一个强类型的array.
function GoAlert()
{
var MyArray = new Array();
for(var i=0 ; i<11 ;i++)
{
MyArray= i+"Element ";
}
return MyArray;
}在托管代码中对它进行调用
object o = HtmlPage.Window.Invoke("GoAlert",NULL);
对返回值进行类型转换
List<string> ReturnList = (List<string>)o; 或者 Array ReturnList = (Array)o;
都会报出异常:
An exception of type 'System.InvalidCastException' occurred in SilverlightPowerful but was not handled in user code
Additional information: Unable to cast object of type 'System.Windows.Browser.ScriptObject' to type 'System.Collections.Generic.List`1[System.String]'.
显然返回的并不是以此方法注释的object类型,而是一个ScriptObject类型.
请问如果来转换?
如果前台JS方法返回一个简单类型,比如一个string,那么托管代码可以轻松o.ToString() 获得.
答案
-
Invoke 返回的是 ScriptObject 对象类型。可以使用下面的方法进行转换:
Code SnippetScriptObject o = HtmlPage.Window.Invoke("GoAlert", null) as ScriptObject;
Array ReturnList = o.ConvertTo<Array>();为了进行检测,我们可以采用下面的方法进行测试:html调用页面(部分代码)
Code Snippet<script type="text/javascript">
function GoAlert()
{
var MyArray = new Array();
for(var i=0 ; i<11 ;i++)
{
MyArray[i] = i+"Element ";
}
return MyArray;
}
</script>
<textarea id="result"></textarea><!-- 注意这里的代码 -->
<div id='errorLocation' style="font-size: small;color: Gray;"></div><div id="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
<param name="source" value="SilverlightApplication2.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>托管代码部分:
ScriptObject o = HtmlPage.Window.Invoke("GoAlert", null) as ScriptObject;
Array ReturnList = o.ConvertTo<Array>();
string s = "";
for (int i = 0; i < ReturnList.Length;i++ )
{
s += ReturnList.GetValue(i) + System.Environment.NewLine;
}
HtmlPage.Document.GetElementById("result").SetAttribute("value", s);
全部回复
-
Invoke 返回的是 ScriptObject 对象类型。可以使用下面的方法进行转换:
Code SnippetScriptObject o = HtmlPage.Window.Invoke("GoAlert", null) as ScriptObject;
Array ReturnList = o.ConvertTo<Array>();为了进行检测,我们可以采用下面的方法进行测试:html调用页面(部分代码)
Code Snippet<script type="text/javascript">
function GoAlert()
{
var MyArray = new Array();
for(var i=0 ; i<11 ;i++)
{
MyArray[i] = i+"Element ";
}
return MyArray;
}
</script>
<textarea id="result"></textarea><!-- 注意这里的代码 -->
<div id='errorLocation' style="font-size: small;color: Gray;"></div><div id="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
<param name="source" value="SilverlightApplication2.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>托管代码部分:
ScriptObject o = HtmlPage.Window.Invoke("GoAlert", null) as ScriptObject;
Array ReturnList = o.ConvertTo<Array>();
string s = "";
for (int i = 0; i < ReturnList.Length;i++ )
{
s += ReturnList.GetValue(i) + System.Environment.NewLine;
}
HtmlPage.Document.GetElementById("result").SetAttribute("value", s);