How to use WebBrowser in windows phone? - c#

I'm using the following code to make my app to load a url,
WebBrowser wb = new WebBrowser();
wb.Navigate(new Uri(uri,UriKind.Absolute));
But still it is not loading that page? What could be the problem??

WebBrowser is a control. Like a button or a textblock, you won't see anything unless you put it somewhere in your page.
To launch an external browser, use WebBrowserTask:
var webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(uri, UriKind.Absolute);
webBrowserTask.Show();

Yo can't have any callbacks with the default WebBrowserTask in wp, if you need more control, use the WebBrowser as you have been doing,
Xaml
<phone:WebBrowser IsScriptEnabled="True" LoadCompleted="UriContentLoaded" x:Name="browserControl" />
Code Behind
public MainPage() //Your page constructor
{
InitializeComponent();
this.browserControl.Loaded += SetBrowserUri;
}
private void SetBrowserUri(object sender, RoutedEventArgs e)
{
browserControl.Navigate(new Uri("http://www.bing.com"));
}
private void UriContentLoaded(object sender, NavigationEventArgs e)
{
if (MessageBox.Show("Do you want to load a second uri?", "Load again", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
browserControl.LoadCompleted -= this.UriContentLoaded; //Remove previous handler
browserControl.LoadCompleted += this.SecondUriContentLoaded; //Add new handler
browserControl.Navigate(new Uri("http://www.google.com"));
}
}
private void SecondUriContentLoaded(object sender, NavigationEventArgs e)
{
MessageBox.Show("Finished loading");
}

Related

WebBrowser.DocumentCompleted and Navigate - C#

I'm trying to modify a simple application which use the WebBrowser item:
private void button1_Click(object sender, EventArgs e)
{
var menu = webBrowser1.Document.GetElementById("SomeItem").InvokeMember("click");
webBrowser1.Visible = true;
button1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Visible = false;
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("domain");
}
This works fine, the page load and then after clicking the button the user is redirected to the desired location.
Now I'm trying to make this without the help of the button.
Simply put the button code inside the load function didn't help, so I've introduced in my code the DocumentCompleted event.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("SomeDomain");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentComplete);
}
private void wb_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
wb.Document.GetElementById("SomeItem").InvokeMember("click");
}
By debugging it, I saw that the wb object is correctly instantiated with the "domain" but I got System.NullReferenceException when I try to retrieve SomeItem.
The document is not fully loaded because I'm still in the load function ?

Retrieving the URL of a clicked link in a C# WebBrowser control

Okay. So, I'm making my own internet browser and I have tabs. But, I'm trying to make it a real time url updater while if you click on any link it will show up in the textbox of the url.
It will not work.
Here is the tab button.
private void button8_Click(object sender, EventArgs e)
{
WebBrowser Browser = new WebBrowser();
tabControl1.TabPages.Add("New Page");
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
Browser.Name = "Web Browser";
Browser.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browser);
((WebBrowser)(tabControl1.SelectedTab.Controls[0])).GoHome();
And here is where my textbox isn't getting the right url.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
//textBox1.Text = Browser.Url.ToString();
((WebBrowser) (this.tabControl1.SelectedTab.Controls[0])).Url.ToString();
}
In order for this to work, you need to get all link elements of the web page you've just loaded and assign a custom function to the HtmlElement.Click event of that element.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
var linkElements = Browser.Document.GetElementsByTagName("a");
foreach(HtmlElement link in linkElements)
{
link.Click += (s, args) =>
{
// a link is being clicked
// get the url the link is pointing to using the href attribute of the element
textBox1.Text = link.GetAttribute("href");
}
}
}

sound not playing at the right moment

I created a MediaElement for a Background Music in my Windows Phone App and it plays perfectly
now the problem when I tried to add sound for a button when pressed
When I debug , the Background music plays , but when I press the button it navigates me to the next page without playing the sound I created, and when I press BackButton from the Navigated Page, it plays it, why?
code:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
}
you should handle this outside button click,
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
Add an event handler to the click and call separately,
Click.MediaEnded += Click_Completed;
void Click_Completed(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
or you can make MediaEnded event of the mediaElement and there in you can call the navigation
Like this:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.MediaEnded += Click_MediaEnded;
Click.Play();
}
void Click_MediaEnded(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}

C#: Stop loop until an event

I have a for loop and inside there is a navigate method for a browser. and it's suppose to load diffrent sites, but the problem is that it will start to load 1 site and before it will load it, it'll load another site. so I need to like pause it until it's completed.
I started to write an event to when the ProgressChanged event is at 100%.. than I figured I don't have any idea what to do next but I think it's a start.
Please help, Thanks!
Edit: I am using Forms as Roland said.
I assume you are doing windows forms programming. The event you want is DocumentCompleted Here's an example:
public Uri MyURI { get; set; }
public Form1()
{
InitializeComponent();
MyURI = new Uri("http://stackoverflow.com");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Url = MyURI;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(e.Url == MyURI)
MessageBox.Show("Page Loaded");
}
For a list of URIs it's straight forward.
public int CurrentIndex = 0;
List<Uri> Uris;
public Form1()
{
InitializeComponent();
Uris = new List<Uri> { new Uri("http://stackoverflow.com"), new Uri("http://google.com/") };
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Url = Uris[CurrentIndex];
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if (e.Url == Uris[CurrentIndex])
{
CurrentIndex++;
if (CurrentIndex < Uris.Count)
{
browser.Url = Uris[CurrentIndex];
}
}
}

C# WebBrowser Controll with javaScript and html

I try to use the WebBrowserControll with C#. I use the following Code.
The WebBrowser Controll open the Web Page and if there is a href= blank it is also oppend in an new WebBrowser Controll. But If I want to open an new JavaScript with window.open it doenst open an new WebBrowser Controll. What I have to do that a href= blank and a Java Script is opend in my WebBrowser Controll.
Does anyone have a Answer??
Thanks for your help.
public MainWindow()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.FileDownload += new EventHandler(webBrowser1_FileDownload);
webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);
webBrowser1.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
this.FormClosing += new FormClosingEventHandler(webBrowser1_FormClosing);
}
public void setURL(String aURL)
{
webBrowser1.Url = new Uri(aURL);
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
// open href= blank in new WebBrowser Controll
MainWindow newWindow = new MainWindow();
newWindow.setURL(webBrowser1.StatusText);
newWindow.Show();
e.Cancel = false;
}
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Console.WriteLine(e.KeyCode.ToString() + " " + e.Modifiers.ToString());
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
{
MessageBox.Show("ctrl-v pressed");
}
}
private void webBrowser1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show(this, "Really close the window?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
private void beendenToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
I Do not think it is possible... The browser should send a create argument (1), when clicking on a window.open, all though it does work when closing a window (destroy (2))
If you are up for a hack you could search the document on load and replace the window.open in the DocumentText property, with tags instead, it should work with a bit of creativity.
Good luck

Categories