How to handle webexception in wpf Frame control? - wpf

I'm using Frame control to display the webpage in wpf app but it throws WebException(Can't locate the remote server) if there is no internet connection.
How can I handle the WebException?

IIRC, the Frame control actually hosts an ActiveX control for the web browsing. When you set the Source property or call the Navigate method, it will asynchonously attempt to navigate the URI.
According to this MSDN post, you'll need to register to handle this asynchronous exception on the Dispatcher.UnhandledException event.

Related

subscribe to clipboard event from within browser

Using the Win32 API it is possible to subscribe to clipboard events (Clipboard event C#).
Is it possible to do this from within an Active X Control or SilverLight within a web page?
My scenario is that I would like to know when something new has arrived in the clipboard but I am in a web page within Internet Explorer (min version 8).
In Silverlight 5 you can enable trusted applications in browser:
http://msdn.microsoft.com/en-us/library/gg192793(v=vs.95).aspx
In trusted applications you can use p/invoke to call WinAPI.
The answer is no you cannot #Vlad I was looking to be notifed when something new has arrived - it doesn't seem possible

SL5 In Browser WebBrowser Control error "Trying to revoke a drop target that has not been registered" when re-navigating to another PDF

Finally bit the bullet and replaced my hack for displaying PDF content inside a silverlight app with a proper SL5 Trusted In-Browser WebBrowser that navigates to a .pdf.
When i try to RE-navigate the WebBrowser to another PDF I am seeing the error "Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100)"
This only happens when I deploy to IIS (localhost IIS and another server IIS 7.5) ... NOT while running in VS2010.
Details:
UI is extremely simple. 1 Grid w/ 2 columns:
Column 1 Column2
======== ====================================================================
Treeview <WebBrowser x:Name="ContentWebBrowser" Grid.Column="1"></WebBrowser>
When user selects an item in the treeview, I simply call:
Uri serverUri = new Uri(string.Format("http://{0}:{1}/",
Application.Current.Host.Source.DnsSafeHost,
Application.Current.Host.Source.Port));
var relative= "/Content/x/y/z.pdf"; //This is dynamiclly pulled from Treeview, EDITED FOR SO
Uri relativeUri = new Uri(relative, UriKind.Relative);
ContentWebBrowser.Navigate(new Uri(serverUri, relativeUri));
With Cassini running silverlight both w/ and w/o debugger via VS2010 i can Select item after item in my list view and my WebBrowser will "re-navigate" calling this same code above.
BUT, once i deploy to my local iis, any time i end up calling Navigate for the 2nd time I am getting this error:
System.Exception: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100)
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.WebBrowser_Navigate(DependencyObject obj, Uri source)
at System.Windows.Controls.WebBrowser.Navigate(Uri source)
at SystemUtilities.Views.WebContentListView.TreeViewItem_Selected(Object sender, RoutedEventArgs e)
Other oddities: Sometimes i can "re-navigate" 3 or 4 times before i see this exception. Sometimes when i load another page in my SL application this error gets thrown. Happens whether PDF is cached or not cached when I load it into the WebBrowser. If i display standard html or an image instead of a PDF this does not happen.
Edit: I added logic to check to make sure LoadCompleted event fires before i try to re-navigate the WebBrowser but it does not seem to matter. (not 100% sure but i think that it ignores the fact that the PDF might still be loading and just signals that the DOM is ready). Although, even if I want a long time (30 seconds) after it looks like the 1st pdf is done loading, i still get the error.
Any suggestions?
New I posted my sample on skydrive.. check out the readme in order to repro!: http://sdrv.ms/JSn1KB

How to get the response headers while the user navigates from page to page?

I have a WPF webbrowser control which navigates the user to a registration site. After the user registers, a collection of headers are sent back alongwith the "finished registration" page.
I have tried the Navigated event, and tried accessing WebResponse property in the event arguments but it's always null.
Is there any other way to access the headers sent?
After quite a lot of reading, I figured out that WPF web browser actually wraps around the WinForm web browser which inturn wraps around a COM dll (which is even used by Internet Explorer).
Now this COM dll provides a lot of interfaces which are not exposed by any of the wrapper controls. So those who need more control need to implement these interfaces.
I found a browser which implements a lot of these interfaces and neatly provides a lot functionality. Used it and it's working fine. http://www.codeproject.com/KB/miscctrl/csEXWB.aspx
It talks about implemnting a PassthroughAPP package to get the response headers.
I dont think you can access headers like that, best thing to do is, either use Hidden Fields of the form or you can pass on your cookie ASP.NET_SessionID from your finished page to your WPF app, and inside WPF app you can make custom request to your url with same ASP.NET_SessionID cookie where you will continue same session and you will be able to query responses you desire throught WebResponse.

WPF WebBrowser events for HTTP errors?

I have a WPF application and I'm using the WebBrowser control to display some content from a public website.
Sometimes in weird edge cases when the network connectivity is flakey, the web browser will show the "This program cannot display the webpage" error page. Is there some way to listen to the WebBrowser to detect when this occurs, so I can load a placeholder?
You could try using the NavigationService from System.Windows.Controls.Frame as indicated in this MSDN forum post. The WebResponse will always be null for the WebBrowser control in WPF (as described in the post).
In the Navigated event arguments, you can access the WebResponse, so you can access the HTTP response code.

Cross-site images in Silverlight

Is it possible to load in an image from another domain using JavaScript, and then pass the image to a Silverlight control on my page?
The only workaround I'm familiar with for this, since it's a security issue, is to set up a web service on a server you have control over, request the image from this web service from your Silverlight application, then have the service grab the image from the other server and return it to the client.
Loading cross-domain images should not be an issue in Silverlight, at least if you are using the normal Image control. I just tested this with a really simple application containing only an Image control with the Source property set to an image from google images.
Are you loading your images in some funky way with the WebClient? Cross-scheme and cross-zone access is restricted for the Image class, so you would not be able to load an image (on any domain) over SSL if you loaded your XAP over regular HTTP.
The MSDN page on Silverlight URL Access Restrictions gives you a good overview of what you can and cannot do with Image, MediaElement, WebClient, and streaming video.

Resources