4

Is it possible to access local Windows Phone Silverlight C#/.NET objects from Javascript loaded in a WebBrowser control in a WP7 application?

2 Answers 2

12

Not directly, but javascript in the WebBrowser can call out to the application asynchronously by using window.external.notify. The application can detect those notifications using the WebBrowser.ScriptNotify event, and calling back to the javascript using WebBrowser.InvokeScript.

Here's an (untested) example.

HTML:

<html>
<head>
<script type="text/javascript">
    function beginCalculate()
    {
        var inputValue = parseInt(document.getElementById('inputText').value);

        window.external.notify(inputValue);
    }

    function endCalculate(result)
    {
        document.getElementById('result').innerHTML = result;
    }
</script>
</head>
<body>
    <h2>Add 5 to a number using notify</h2>

    <div>
        <input type="text" id="inputText" />
        <span> + 5 =</span>
        <span id="result">??</span>
    </div>

    <input type="button" onclick="beginCalculate()" />
</body>
</html>

Application:

/// <WebBrowser x:Name="Browser" ScriptNotify="Browser_ScriptNotify" />

private void Browser_ScriptNotify(objec sender, NotifyEventArgs e)
{
    int value = Int32.Parse(e.Value);

    string result = (value + 5).ToString();

    // endCalculate can return a value
    object scriptResult = Browser.InvokeScript("endCalculate", result);
}
Sign up to request clarification or add additional context in comments.

Comments

0

if you are using the WebBrowser of WP7 to connect to a remote web site (and this must be the case and WP7 does not host a web server), you can test the web application also from a normal desktop.

Usually if you need to communicate between client-side (JavaScript) and server-side (C# in your case) depending on the specific context and needs you can use some different techniques, for example Page Methods (google for ASP.NET page methods).

it's my understanding that WP7 browser is somewhere between IE8 and IE9 with slightly limited JavaScript engine compared to the both, but something so basic like Page Method should work, I would then test it first from a normal PC then from the Phone and verify if works and if not what breaks.

5 Comments

I'm sorry if my question was not clear, but I meant accessing the C# objects in the WP7 application code inside which the WebBrowser control is placed. My goal is to fire custom WP7 events when the user triggers Javascript events inside the WebBrowser control.
JavaScript works in the browser, C# lives on the server. How do you imagine to break the bound between web browser and outer World? Are you embedding the browser inside a SL WP7 application?
Yes, the browser is inside a SL WP7 application.
There is potential confusion here - the WP application most likely uses Silverlight/C#. So the C# lives on the phone, but outside the WebBrowserControl.
you can have windows phone apps writting fully in javascript. these hybrid apps work just the same as on ios and android; living inside a browser component, utilizing AppCache for offline functionality. these apps can call out to .NET (C# or whatever) code through a proxy object. see the accepted answer.

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.