I have a Silverlight application written in C# that utilizes the Bing Maps SDK. In this application I have a page with some javascript functions to expose functions within the application. The application registers the Bing MapControl in the MainPage's constructor with:
HtmlPage.RegisterScriptableObject("Page", new MapControl(this.BingMap));
In the javascript function, I call the methods of the application like so:
<script type="text/javascript">
function ClearMap() {
var control = document.getElementById('silverlightControl');
control.Content.Page.ClearMap();
}
</script>
However, whenever I refer to the control.Content.Page property in a javascript function, I get an "Unspecified error". Even just setting it to a variable will cause the error, for example:
var page = control.Content.Page;
Here is the strange part. I am using this silverlight application from 2 other applications. One is a windows service, and the other a winform application. The winform embeds a visible WebBrowser into its form and uses that to call the javascript functions. When using the winform, everything works fine with no errors.
The error only occurs when calling these functions from the windows service. The service instantiates a WebBrowser object just like the winform application does, and initializes everything in the same way. The only difference is that it's running as a service, and the WebBrowser is not visible nor embedded in any forms. Could that fact possibly cause this error?
I currently have the winform application, the windows service, and the silverlight application's webservice all running on my own local machine.
UPDATE
After Gnostus' comments, I figured out that the main issue I'm having is that the "Page" object is not being created because the App class never gets initialized after navigating to the webservice from the windows service-based WebBrowser. The winform app and the service both do this the exact same way:
WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
WebBrowser.Navigate("http://localhost:56062/BingMapsWebServiceTestPage.aspx");
while (WebBrowser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
I added some code to the MainPage's constructor where it calls HtmlPage.RegisterScriptableObject("Page", new MapControl(this.BingMap)); that will log some text when it gets hit. When I run the above code in the WebBrowser of the winform application, the following occurs:
The DocumentCompleted event gets fired twice (once by the browser and I assume once by the silverlight control)
The App() is intialized and the "Page" element successfully added in the MainPage's constructor
The above while loop completes
When I do the same from the windows service, it does #1 and #3, but not #2. Step 2 only gets hit when I call a javascript function that refers to the Page object, and only after it errors.
UPDATE 2
After messing around in a new winform application, I discovered that the silverlight control will fail to load upon calling Navigate() whenever the WebBrowser or its parent form is invisible. I found that it's possible to still call my javascript functions if the parent form is hidden AFTER the silverlight application has finished loading. Does anyone know a better way to force the silverlight app to load aside from flashing up a visible form? Is it even possible to show a form from a windows service, or am I going to have to rethink this whole thing as a console application?
Related
I am developing a web view universal windows application. In fact my mobile ready website will be shown within a web view or webbrowser control in windows universal application..
Using the website directly from Edge in mobile (or chrome in desktop etc) the app notifies the user to allow using location as it should (HTML Geolocation API ).
However in my app using web browser control this is not happening. It is not able to run the script or something.
I have given grant access to user's location in the app but still no action is taken.
What else can I do in order to get user's location within an app that uses webbrowser ?
I found the solution.
You have to fire the PermissionRequested event. And in this event you have to execute the Allow method of the WebViewPermissionRequestedEventArgs object. Something like this:
private void webView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
if (args.PermissionRequest.PermissionType == WebViewPermissionType.Geolocation)
args.PermissionRequest.Allow();
}
The first page in Windows Phone is always cached, that means when you run the app then press back button to close the app then reopen the app from the phone or the simulator (not from visual studio) the page events such as the constructor, NavigationHelper_LoadState, and OnNavigatedTo are not called, even I disabled page cashing by
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Disabled;
and in App.xaml.cs
rootFrame.CacheSize = 0;
But still cached.
How can I solve this problem because I need to get data from a server and show it in the page.
Couldn't you load data in OnNavigatedTo? If you need to load data in constructor you can also try to remove page from navigation stack.
I have a multiview: view1 contains a button and view2 contains the silverlight app - basically a button.
Pressing the asp:button in view1 takes you to view2 and the silverlight app is activated.
How do I make the click of the button in the silverlight app take me back to view1.
I tried to use a WCF service with aspnetcompatibility enabled but that has not yielded results since httpcontext.current.response is null.
Maybe I should use an httphandler?
I was wondering if someone could guide me in the right direction.
Thanks.
You can write a javascript method in the Silverlight hosting page and you can invoke the JS method using
HtmlPage.Window.Invoke("YourJSMethod", parameter);
In the YourJSMethod() javascript method you can write the logic to load the view 1 back.
I have an application that connects to www.Jango.com and using a web browser control. However when using the .NET browser control I constantly receive a script error of “Unable to get value of the property 'msIsSideMode': object is null or undefined”, and therefore the rest of the site does not load.
This can be reproduced by creating a simple windows forms application, adding a webbrowser control and navigating to jango.com
As far as I can tell, the web browser control renders a website based on the version of IE installed on your machine. On my machine I have IE9 installed. The method msIsSiteMode appears to be an IE method that tells weather the current page was launched as a pinned site. Since jango.com is very JavaScript based, this null value causes the website to stop functioning correctly. However navigating to jango.com in Internet Explorer works just fine and no errors are produced.
Is anyone aware of a way to work around this, such as having my application set the value? Have the web browser control render as a different version. Any suggestions at all, I am open to anything, except changing the version of IE on my machine as this is not an option.
Set the ScriptErrorsSuppressed property to True on the WebBrowser control.
EDIT: One more alternative
Try this on the Form that hosts the web user control:
using System.Runtime.InteropServices;
using System.Security.Permissions;
[ComVisibleAttribute(true)]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ObjectForScripting = this;
}
...
}
Read here for more detailed information.
I see similar problem in WebBrower Control with IE9 installed on my machine. In my case, I have two windows forms with WebBrowser Controls. One of those is used to load the pop up from the other.I get the script error on pop up one.its only with IE9
I have a C# app that uses a web browser control to display some HTML and do some JScript in the background.
How do I integrate this with other system calls? Do I call other C# code from JScript/VBScript? Do I need to do it from the form itself?
When I need javascript hosted in a webbrowser control to call back to the parent, I have it try to navigate the page to another URL. Then in the webbrowser's "BeforeNavigate" event, I get the URL the page was trying to go to, parse out any arguments, dispatch the request to the other C# code, then cancel the original navigate request.