Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF VB.NET webbrowser control, showing a local HTML file with album covers as jpg images. All album covers have a link to a text file containing the albumdata. When clicked, a listbox is populated with the items in the text file.

Currently I am using the Webbrowser Navigating event to retrieve the clicked link as (e.uri). However, I also want to show the album cover next to the listbox as well. So I need to retrieve the path to the clicked image.


The current code:

VB
Private Sub WhatsNewBrowser_Navigating(sender As Object, e As NavigatingCancelEventArgs) Handles WhatsNewBrowser.Navigating
If e.Uri.ToString.EndsWith(".txt") Then
            e.Cancel = True

            Dim dlg As New WindowWhatsnewAlbumViewer()
            dlg.Owner = Me
            dlg.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner

            Dim image As New BitmapImage
            image.BeginInit()
            image.UriSource = New Uri("Path to my image goes here")
            image.EndInit()
            dlg.ImgAlbumCover.Source = image

            dlg.ShowDialog()
End If
End sub


How do I retrieve the path to the clicked element?
Posted

1 solution

This approach might help you: http://stackoverflow.com/questions/9110388/web-browser-control-how-to-capture-document-events[^]

It looks, like WPF WebBrowser control is facing even vorse flaws than the Windows Forms version. The concept of the solution on the link is to directly hook into the DOM with an event handler created on your app side.

Please note the comment below the accepted solution. It is linking here: http://www.codegain.com/articles/wpf/ieprogramming/working-with-webbrowser-in-wpf.aspx[^]. There are many interesting things, but you should look at Invoke C# method from Javascript section. In your case this could be an even better approach, since you are using the browser control only as a layout engine.

But you might consider taking an other path: you have a layout engine already, WPF itself! Just recreate your local html as xaml, and load it from code in a container. And you have everything you need, even more.
 
Share this answer
 
Comments
fanoftheplanet 18-Nov-14 15:33pm    
I tried the approach of attaching an event handler, but I came only across c++ code that I was only partly able to convert (just like the references you sent). So I asked in a different thread, and we came up with this solution. But it is only useful for the "clicked link" part. It does not give me the link to the image itself.

I agree the wpf browser is a real pain with many flaws, and I agree that I could better use plain wpf instead. But the problem is, the html page I am loading contains a fancy javascript image carrousel, that I can manage externaly (change, add, replace, etc...). If I would hardcode a carrousel into my application, there would be no easy way of changing the layout and behaviour. Now I only need to update my HTML page to instantly change the look and behaviour with all my clients.
Zoltán Zörgő 18-Nov-14 15:41pm    
At the point you have the cliked link, and you have access to the DOM, you have what you need. But if you use the approach allowing you to call a .net method directly from javascript you don't need to. On javascript side simply gather all information you need and pass it to the application. That's all.

Please share a portion of your html and explain what exactly do you want to process from it in your app.

On the other hand there are many fancy WPF carousels out there :)
fanoftheplanet 18-Nov-14 16:02pm    
Imagine a link like:
<img src="c:\img23.jpg">


When I click on img23.jpg, the navigating event e.uri gives me "c:\album3.txt", but what I also need is the path to "c:\img23.jpg".

That's basically it.

Regarding the WPF carousels: Would it be possible to load a carousel from external code (so not being part of my app), or would the carousel be hardcoded? I will definately take a closer look at this option.
Zoltán Zörgő 18-Nov-14 16:13pm    
So you have an image in a link. Both approaches will work. Having the first one (sorry for c#, but the idea is the same):
<pre>
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
...
return true;
}
</pre>
IHTMLEventObj defines srcElement, this will be the <pre>A element. It's child will be the image.

For the carousel part: you can load XAML at runtime and have events attached. So you can have part of your application compiled and part of it in XAML form.
Zoltán Zörgő 19-Nov-14 4:11am    
Have you suceeded?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900