c# - WPF WebBrowser - open links in default browser -


i using wpf system.windows.controls.webbrowser control show html content downloaded service. html contains urls ("a" elements) should clickable.

by default, when such url clicked opens in internet explorer. want them open in default browser instead.

note talking wpf webbrowser. there tons of solutions winforms browser, not work wpf browser.

the common "solution" handle navigating event, cancel , own thing url. does not work because navigating event not called when click link in html.

another solution found seem work, strange reason: https://stackoverflow.com/a/9111151/573249

i have following code, using method link above:

private void webbrowser_onnavigating(object sender, navigatingcanceleventargs e) {     // demonstration purposes     // not called when link clicked      debug.writeline("> navigating called.");      if (e.uri == null)     {         debug.writeline(">> uri null.");         return;     }     e.cancel = true;     process.start(e.uri.absolutepath);     debug.writeline(">> navigation cancelled , opened in default browser."); }  private void webbrowser_onloadcompleted(object sender, navigationeventargs e) {     debug.writeline("> loadcompleted called.");      mshtml.htmldocument doc;     doc = (mshtml.htmldocument) webbrowser.document;     mshtml.htmldocumentevents2_event ievent;     ievent = (mshtml.htmldocumentevents2_event) doc;     ievent.onclick += new mshtml.htmldocumentevents2_onclickeventhandler(clickeventhandler);      debug.writeline("> loadcompleted finished!");     debug.writeline("------"); }  private bool clickeventhandler(mshtml.ihtmleventobj e) {     // opens url in default browser     // method called 20% of time.      debug.writeline(">> click event handler called.");      var = (mshtml.htmlanchorelement) e.srcelement;     process.start(a.href);     return false; } 

when click link, seems work maybe 20% of time. in cases, "clickeventhandler" called, , link opened in default browser. in other 80% of cases, "clickeventhandler" never called (and link opens in ie), though "onloadcompleted" finishes without exceptions.

there not seem pattern, although when fails once seems keep failing forever on same document until re-load html. after re-loading 20% chance of working.

what's going on?

hi had same problem. onmousedown event fired sometimes. , couldn't figure out. gave , used winform webbrowser instead. don't need reference mshtml need reference system.windows.forms , system.windows.interactivity.

xaml

xmlns:wf="clr-namespace:system.windows.forms;assembly=system.windows.forms"  <windowsformshost>   <wf:webbrowser x:name="wbthumbs"/> </windowsformshost> 

c#

public wmain() {   system.windows.forms.application.enablevisualstyles();   initializecomponent();    wbthumbs.documentcompleted += wbthumbsondocumentcompleted;   wbthumbs.navigate("www.blabla.com"); }  private void wbthumbsondocumentcompleted(object sender, system.windows.forms.webbrowserdocumentcompletedeventargs e) {   if (wbthumbs.document == null) return;   wbthumbs.document.click += wbthumbs_click; }  private void wbthumbs_click(object sender, system.windows.forms.htmlelementeventargs e) {   var doc = wbthumbs.document;   var src = doc?.getelementfrompoint(e.clientmouseposition);   if (src == null) return;   //... } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -