Question

I'm attempting to use an XBAP to acquire TWAIN Images but I'm not even getting that far. I can't seem to get the BrowserInteropHelper.HostScript to allow me to talk back to the javascript on the host page.

  • I am running the XBAP in an iframe.
  • I tried full trust (even though that should be a requirement).
  • I'm testing on IE9, .NET Framework 4.0
  • The BrowserInteropHelper.HostScript is not null, I can use the normal window methods, like .Close().

My code looks like this:

Index.html:

<p id="someP">My cat's breath smells like cat food.</p>

<script type="text/javascript">
    function WorkDamnit() {
        $('#someP').hide();
    }
</script>

<iframe src="@Url.Content("~/XBAPs/WPFBrowserApplication1.xbap")" style="border: none;" width="500" height="500" />

Page1.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (BrowserInteropHelper.HostScript == null)
        throw new ApplicationException("hostscript is null");
    else
        BrowserInteropHelper.HostScript.WorkDamnit();
}

I get:

System.MissingMethodException: Method '[object Window].WorkDamnit' not found.

New Info:

This scenario works properly from other PCs on the network and I found out why. IE9 has a setting turned on by default called "Display intranet sites in Compatibility View". This causes the page to render in "IE7 mode" and the javascript function is available from the XBAP. If I click Document Mode: (under the F12 developer tools console) and switch to IE9 mode then it no longer works (as above). On my own PC it uses IE9 mode by default (As it should) and it does not work unless I manually switch to IE7 mode.

Was it helpful?

Solution 3

IE9 does not expose BrowserInteropHelper.HostScript unless it is in compatibility mode.

OTHER TIPS

In IE9, window methods are available to you, so you could try setTimeout

BrowserInteropHelper.HostScript.setTimeout("WorkDamnit()",0);

Its due to IE security settings. IE by default blocks the scripts if page is opened from local drive. IE should display the warning message as "'To help protect your security, Internet Explorer has restricted...."

However you can change the settings
Check the checkbox "Allow active content to run in files on My Computer"

IE settings

This is pretty far after the fact but I wanted to chime in because I've dealt with this problem and it's an ugly one.

As you have observed, the machines it runs on work because they are running in compatibility mode. You can instruct IE9 to render your page using compatibility mode by adding the following tag to your html documents:

<meta http-equiv="X-UA-Compatible" content="IE=8"/>

I'm confident in saying, adding this tag is your only 'solution.' I've spent weeks looking for a better one and this is the one I'm still stuck with.

For whatever reason the ScriptInteropHelper is rife with problems in IE9. It hasn't gotten a lot of attention from Microsoft, likely because it's obscure functionality. That said, particularly because IE7 and IE8 have all sorts of quirks, it's extraordinarily annoying you have to run in compatibility mode. There are a number of MSDN articles, for example this one and enter link description here that discuss the issue further.

If someone is still wondering about this problem, I got found out a solution. Basically from document object you can communicate properly without any hacks in latest browsers and with HTML5 doctype.

I wrote a blog post that contains the codes and examples:

XBAP and Javascript with IE9 or newer - Solution for MissingMethodException problems

Quickly explained this works properly:

Javascript

document.ResponseData = function (responseData) {
    alert(responseData);
}

C# XBAP

var hostScript = BrowserInteropHelper.HostScript;
hostScript.document.ResponseData("Hello World");

It can be then used to pass C# object to Javascript and used as "proxy" object. Check that blog post for details how that can be used.

BrowserInteropHelper.HostScript.setTimeout("WorkDamnit()",0);

its working but if i want to send callback functions to js how can i send object with setTimeout ?


when using for ie8 its working good

dynamic host = BrowserInteropHelper.HostScript;
host.sampleJSFunction(new CallbackObject(this));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top