Search code examples
c#javascriptxbap

Custom functions unavailable from BrowserInteropHelper.HostScript in an XBAP


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.


Solution

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