WPF WebBrowser OnTitleChanged Event - c#

i am using System.Windows.Controls.Webbrowser class in my C# app.
I want to recognize if the document title changes.
Therefore i listen on the LoadComplete Event and getting the title
public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = browser.Document;
setTitle(doc.Title);
}
The problem now is, that the title could change during loading. In this case the LoadCompleted event would not fire.
Next problem is. I have a html application inside which navigates through javascript functions in the application. In case of using these javascript links, the event would not fire, too.
I am looking for an event like OnTitleChange from CHtmlView (c++)
Is there a possibilty to solve my issue?
Thanks for help.

Not supported by the WPF webbrowser.
You can use windows forms interop to show a windows forms webbrowser control then subscribe to the DocumentTitleChanged event.

This is the working code for me:
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigate(URL);
browser.DocumentTitleChanged += new EventHandler(browser_DocumentTitleChanged);
WindowsFormsHost winFormsHost = new WindowsFormsHost();
winFormsHost.Child = browser;
AddChild(winFormsHost);
public void browser_DocumentTitleChanged(object sender, EventArgs e)
{
setTitle(browser.DocumentTitle);
}

Related

Back button controlling for my Windows Phone 8.1 Silverlight App

I am developing a Windows Phone 8.1 Silverlight app. In my app, I need to override the back button code.
So I tried this,
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
DisplayPage mynewPage = new DisplayPage();
this.Content = mynewPage;
e.Cancel = true;
}
But this code is not working. What am I doing wrong? Please help me out.
EDIT:
When I place this code on MainPage, it works! But I don't want to place it there. I want to place it on other pages.
Remove "e.Cancel = true" (it cancels the navigation). Simply just navigate to the new page.
EDIT:
For navigating to another page, I would rather suggest using NavigationService. Check out this page for examples.
EDIT:
Code snippet:
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
NavigationService.Navigate(new Uri("/DisplayPage.xaml", UriKind.Relative));
}

WebView Stop Navigation

I'm currently just goofing around, programming an Web Browser.
i wonder how to Stop Navigation from WebView just with a button click?
if there's any bool/void's involved in the solution, explain it please.
Appreciate it!
If you are using WebView from WinRT, you can call WebView.Stop().
If you are using WebBrowser from WP Silverlight, you can subscribe Navigating event and set NavigatingEventArgs.Cancel as true.
Reference:
WebView.Stop Method
NavigatingEventArgs
You can do only using javascript,as below
private void Button_Click(object sender, RoutedEventArgs e)
{
Browser1.InvokeScript("eval", "document.execCommand('Stop');");
}

WebBrowser opens link in IE instead of Default Browser?

So I have a WPF application that opens up a new window. Then on that new window, it creates a webBrowser object:
WebBrowser browser = new WebBrowser();
browser.Source = new System.Uri(chatUrl);
browser.Navigating += new NavigatingCancelEventHandler(browser_Navigating);
this.browserControl.Child = browser;
As you can see, I have created a hook for NavigatingCancelEventHandler. Based on what I've seen, this handler is supposed to intercept links clicked within the webbrowser.
private void browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
System.Diagnostics.Process.Start(e.Uri.ToString());
}
So I take the navigation, cancel it and use the Process.Start function to open it in my default browser. The problem is, it doesn't do this. It still opens up in IE9. I've seen other threads here on StackOverflow, and they all say to do what I'm doing. But what I'm doing doesn't work. Please help.
You're using the incorrect delegate type for your event handler, you're specifying a NavigatingCancelEventArgs but it should be a WebBrowserNavigatingEventHandler.
Easiest way to get the right one is to just type
browser.Navigating +=
And press TAB twice, it'll generate the correct handler for you.
Edit: Hmm, I'm looking at the S.W.F version, but i'd try the above in either case for WPF.

How can I detect when a web page finishes loading in a WebBrowser control?

I'm talking about that "webbrowser" control which uses internet explorer.
How can I detect when it finishes loading?
Always look for events in such cases. In this case, DocumentCompleted event.
Subscribe to the WebBrowser control's DocumentCompleted event. i.e.
this.webBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.WebBrowser_DocumentCompleted);
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Do stuff here
}

WPF WebBrowser Mouse Events not working as expected

I have a WebBrowser object in a WPF Page and I'm trying to do something whenever the user interacts with the page. I have intially tried to use the events associated with the WebBrowser object but they don't seem to be firing. Below is a simplified example of what my code is trying to do:
webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown);
with the event handler as:
void webBrowser_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.MessageBox.Show("Pressed");
}
However when I run the page and click inside the WebBrowser no message box is displayed.
Apologies, originally I had mentioned that it was a System.Controls WebBrowser rather than a Forms browser.
Mouse events are not supported by the WebBrowser control, according to the documentation. You need to hook up handlers to the DOM events provided by the document being displayed in the control, using the WebBrowser.Document property. This post has an example of how to do this.
Add the ms html com library
Once the WebBrowser.LoadCompleted event fires try this:
mshtml.HTMLDocumentEvents2_Event doc = ((mshtml.HTMLDocumentEvents2_Event)Browser.Document);
doc.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(doc_onmouseover);
or use some other event.
Hope this helps someone.

Categories