0

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

4
  • Why are you calling into JavaScript at all? Why not just code it all in silverlight? Commented Sep 11, 2011 at 20:34
  • The API library I have is in JS & the documentation says: "Our formatted responses are designed to work optimally with the JQuery getJSON method." Commented Sep 11, 2011 at 20:49
  • Ok but there is no such thing as "jQuery optimal json". json is a standard and Silverlight has two very good ways to handle json, I would strongly recommend you use them than make blocking calls into JavaScript. Commented Sep 11, 2011 at 21:09
  • Well primarily it's for code-reuse in our PHP version of this product. Additionally, this feature-add began with the Google Adwords JavaScript API, which is what got me into JS to begin with. Commented Sep 11, 2011 at 23:22

2 Answers 2

0

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.