Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSBridge doesn't work for Razor views #4

Open
RodH257 opened this issue Sep 30, 2013 · 2 comments
Open

JSBridge doesn't work for Razor views #4

RodH257 opened this issue Sep 30, 2013 · 2 comments

Comments

@RodH257
Copy link

RodH257 commented Sep 30, 2013

Hi,

I'm attempting to use this cool library in an app that users Razor views (which is a very cool feature of Xamarin) however I don't seem to be able to get it working.

Looking at the code, it seems that it because JSBridge uses a protocol handler, perhaps it cannot work with LoadHtmlString, and instead needs an actual request ?

A workaround might be for me to save my razor generated HTML as a file on disk and then load it, but seems like it might be slow. Or I could have a basic HTML file then load in via the JSBridge the rest of the DOM generated from Razor?

Just wondering if I'm missing something and there's actually a way for it to work easily?

Thanks!

@cdeutsch
Copy link
Owner

Good question Rod. I've never tried using LoadHtmlString. I'll have to play around with it.

For JsBridge to work mt.js has to be loaded in your HTML. So make sure that's happening. You could try do an alert(Mt.appId); to make sure.

It may be a few days before I can test it out but I'll let you know what I find.

@RodH257
Copy link
Author

RodH257 commented Sep 30, 2013

Thanks Christopher,

I have confirmed that the mt.js is loaded through the alert as you suggested, but still no go. My workaround for the moment is this:

<!doctype html>
<html>
    <head>
        <title>Buildaform</title>
      <script src="js/mt.js"></script>
    </head>
    <body>
        <div id="main">
            <p>Loading ...</p>
        </div>
        <script>
            window.onload = function(e) {
                Mt.App.addEventListener('loadPage', function(data) {
                    document.getElementById('main').innerHTML = data.HTML;
                });
                Mt.App.fireEvent('pageLoaded', { });
            };
        </script>
    </body>
</html>

and then in my controller:

string path = NSBundle.MainBundle.PathForResource( "www/index", "html" );
string address = string.Format("file:{0}", path).Replace( " ", "%20" );
webView.LoadRequest(new NSUrlRequest(new NSUrl(address)));
webView.AddEventListener( "pageLoaded", delegate(FireEventData arg) {
    Console.WriteLine("Loading razor");
    var template = new NewProjectView () { Model = detailItem };
    webView.FireEvent( "loadPage", new {
        HTML = template.GenerateString()
    });
});

Not as ideal but it gives me he ability to generate Razor views and still make use of this awesome JSBridge library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants