How to open links in wpf webview in default explorer - c#

My webview display a html page which contains lots of links, if I click the link, the new page will open in the webview, is there a way to open the link in default browser rather than in webview?

You can handle the WebView.NavigationStarting event as follows:
private void navigationStartingHandler(object sender, WebViewNavigationStartingEventArgs e)
{
e.Cancel = true;
Process.Start(e.Uri.ToString());
}
Here and here are more answers about opening web pages in default browser

Related

problem of opening a webPage in webBrowser c# control in place of IE control

For my project I used webBrowser control and when I launch an html site which has got an input(type) button with onclick javascript function which process ending on window.open target="_blank", it's internet explorer window that's is opening in place of a new page of webBrowser.
I program in c# code with winform technology and I used webBrowser System.Windows.Forms.
So which function could I used to open the page of this site in webBrowser window in place of IE window.
Thanks you for your help.
M.A.
Read about WebBrowser.Navigating event:
You can handle the Navigating event to cancel navigation if certain conditions have not been met, for example, when the user has not completely filled out a form. To cancel navigation, set the Cancel property of the WebBrowserNavigatingEventArgs object passed to the event handler to true. You can also use this object to retrieve the URL of the new document through the WebBrowserNavigatingEventArgs.Url property.
To open page in default browser, for example:
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
Process.Start(e.Url.ToString());
}
Finally,
I used the package reference "SHDocVm" of "Microsoft Internet Controls" and I place a function handler NewWindow3 in the function "DocumentCompleted" of webBrowser and it's ok, a window.open() javascript function with target="_blank" opens a new page of webBrowser.
Truly yours.
M.A.

Opening Web Browser click in default browser

Currently I am building a windows form app using c#. I have a web browser control on my form that displays a google ad. When clicked the webpage is displayed within the little 300x300 web browser control on the form. Is there a way for me to launch the default browser when the ad is clicked instead?
Thanks.
Edit: I figured out I can do so open the default browser by using Process.Start("url here"); but the browser windows is opening upon app load.
Edit Adding Code:
Form.cs
private void AdPanelNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
if (e.Url != null ) Process.Start(e.Url.ToString());
}
Form.Designer.cs
this.AdPanel.Navigating += new WebBrowserNavigatingEventHandler(AdPanelNavigating);
You can add Navigating event handler:
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(WebBrowser_Navigating);
void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
e.Cancel = true;
Process.Start(e.Url);
}
It will open default browser instead of opening this url inside webbrowser.

Detect a specific website my WebBrowser navigated to

I'm using a WebBrowser Control on a WPF application that opens a website with a form for the user to fill and submit. After submitting, it redirects to another page after 10 secs, www.google.com for example.
Is there a way to detect when the WebBrowser has opened www.google.com?
You can detect when the WebBrowser has opened your site name in webbrowser_LoadCompleted event,try this:
void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc =( mshtml.HTMLDocument) s.Document;
if (doc.parentWindow.window.location.host == "www.google.com")
{
MessageBox.Show("you navigated to google");
}
}

Metro App handle open link / url loading in webview

i have a banner which contain click zone to url like this :
playMusic://123456
How can i catch link opening or url loading for manage this type of link into the application?
it seems that there is no webview event for this, and I have not managed to find a override method.
Thanks.
I hope that your webview dynamically changes the url, and you need to catch the dynamically navigated url.
If so,
WebView1.LoadCompleted += WebView1_LoadCompleted;
void WebView1_LoadCompleted(object sender, NavigationEventArgs e)
{
new MessageDialog(e.Uri.ToString()).ShowAsync();
}
This code will display Message Box of the url loaded when webview navigated to an url...
If this is not you looking for, pls elaborate your scenario ..

C# Intercept Browse Button

Have a unique customer request which Im unsure how to tackle.
The customer has a webpage form with a browse button to select a file. When the browse button is clicked, instead of showing the local files, they want to pop-up a window with a textbox to enter a code. This code is then used to select a file from a local folder containing 1000 files each with their own code. They want to prevent the user from viewing the other files in that folder.
I did write a custom Windows form to mimic the webpage form but they already have the webpage online and would like to reuse it.
Any ideas how to intercept the browse button? I can use a C# Application with the web browser component, but can that intercept the browse button?
The only option that I can see working is using a C# Application with the web browser component. You can then use WebBrowser.ObjectForScripting to provide a method that can be called to trigger your custom picker window through Javscript, e.g:
window.external.ShowPickerWindow();
You then have two options:
Interrogate the DOM of the page once it's loaded and replace the button with one that triggers your picker window.
Have the customer change their page so it checks for the existance of a window.external.ShowPickerWindow method and basically does option (1) for you.
You can then have a method, perhaps called window.external.GetPickedCode() to pull the code out in the page.
Rob kinder steered me along the correct thinking track by saying "replace the button" which has lead me to a solution which works beautifully!
In short, I hide the browse button, insert a new button next to it that when clicked, opens a new window with a textbox. This textbox then sets a string value in the parent form which is used onSubmit to attach the file.
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement btnBrowse = wb.Document.GetElementById("fiPhoto");
if (btnBrowse != null)
{
HtmlElement newbtn = wb.Document.CreateElement("input");
newbtn.SetAttribute("id", "btnLoad");
newbtn.SetAttribute("type", "button");
newbtn.SetAttribute("value", "Load");
newbtn.Click += new HtmlElementEventHandler(newbtn_Click);
btnBrowse.Parent.AppendChild(newbtn);
btnBrowse.Style = "display:none";
}
HtmlElementCollection forms = wb.Document.Forms;
if (forms.Count > 0)
{
HtmlElement form = wb.Document.Forms[0];
form.AttachEventHandler("onsubmit", delegate(object o, EventArgs arg)
{
FormToMultipartPostData postData = new FormToMultipartPostData(wb, form);
postData.AddFile("photo", photo);
postData.Submit();
});
}
}
private void newbtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
}
FormToMultipartPostData is too big to post in here but it basically manually constructs the Content-Disposition to be posted
Don't show the actual file browser, imitate one which is showing only that one file in in.
Or since you know the file path when correct code is entered copy the file to temp folder you created and open file browser to browse that folder and it will be contain only that file.

Categories

Resources