JsBridge
JsBridge copied to clipboard
JSBridge doesn't work for Razor views
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!
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.
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.