开发者

Return object from javascript to silverlight

开发者 https://www.devze.com 2023-04-04 14:24 出处:网络
Here are the steps I\'d like to take: 1) User enters search term into box in silverlight, then presses enter

Here are the steps I'd like to take:

1) User enters search term into box in silverlight, then presses enter

2) Search term is passed into javascript via C# code: HtmlPage.Window.Invoke("CallAPI", SearchText);

3) CallAPI function hits the API via $.getJSON & returns a value into the JS callback function [this is already done]

4) Resultant object is returned to the Silverlight/C# page for display in silverlight UI

I can do everything except step 4. How do I get a JSON object from Javascript into C#? I've been working on this for the last few hours, and this is what I thought would do it:

ScriptObject myScriptObject = (ScriptObject)HtmlPage.Window.Invoke("CallWordStreamAPI", SearchText);

I set a breakpoint in my JS & validated that the object in 开发者_开发技巧my return statement is definitely populated with the 20 rows of data, as expected.

I set a breakpoint in my C# (ScriptObject myScriptObject = ....), and myScriptObject is null after the call. If I set a breakpoint in firebug/chrome dev at the line "return r" (my object), I can see there are 20 items listed in r.data. If I set a breakpoint after the myScriptObject line listed above, myScriptObject is null.

Your help is appreciated.

Scott


I was calling this from a ViewModel on the server side. I ended up using MVVM Messaging to send the keyword over to my code behind on the client side. I then called my JS function, returned the result, and shot a message back into my view model.

Aside from that, the syntactic issues were resolved here:

How can I pass a JavaScript function to Silverlight?


My Code:

<!-- language: JavaScript -->
function sendText() {
    return "Hi from Javascript!";
}

<!-- language: C# -->
string obj = HtmlPage.Window.Invoke("sendText", null) as string;
txtReturnData.Text = obj;

<!-- language: VB.Net -->
Dim obj As String = TryCast(HtmlPage.Window.Invoke("sendText", Nothing), String)
txtReturnData.Text = obj
0

精彩评论

暂无评论...
验证码 换一张
取 消