March 18, 2013

first app?

Question mark because all I pretty much did was follow the directions on here, and all that amounted to was dragging a textbox, dragging a button, and dragging a webBrowser control onto a blank app and copying and pasting four lines of code.  Guess we all gotta start somewhere eh?

So here's the code that Microsoft provided for us:
    private void Go_Click(object sender, RoutedEventArgs e)
    {
        string site = URL.Text;
        MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));
    }
I also tried
MiniBrowser.Navigate(new Uri(site));
MiniBrowser.Navigate(new Uri(site, UriKind.Relative) // this hated me
MiniBrowser.Navigate(new Uri(site, UriKind.RelativeOrAbsolute))

I noticed, that for (site, UriKind.Absolute) and just (site), if I removed the "http://" from the website address ("www.bing.com" instead of "http://www.bing.com") then the emulator would take me to an Application_UnhandledException.  But removing the "www" didn't produce errors ("http://bing.com"), while removing both the "www" and "http://" produced errors, but that's because the "http://" was missing.  (site, UriKind.Relative) didn't work for anything, but when I removed the "http://" then it rewrote the MiniBrowser with a "We're having trouble displaying this page" instead of throwing an exception.  So when I tried the (site, UriKind.RelativeOrAbsolute) for the above options, it didn't throw an exception but went into the "We're having trouble displaying this page", which I prefer.  Of course when a null address was input, then it threw an exception for all of the cases.

So taking these into account, here is a more robust version:
    private void Go_Click(object sender, RoutedEventArgs e)
    {
        string site = URL.Text;
        if (String.IsNullOrEmpty(site)) return;
        if (!site.StartsWith("http://") && !site.StartsWith("https://"))
            site = "http://" + site;
        MiniBrowser.Navigate(new Uri(site, UriKind.RelativeOrAbsolute));
    }

And here is a picture:
(bing gets jealous when I use this app and launches itself in full page version.  I took this screenshot in a two-second window.  This app works for other sites though.)