//method is called if webpage couldn't be loaded private async void MainWeb_NavigationFailed(object sender, WebViewNavigationFailedEventArgs e) { //we want to display the error page that is saved in app installation directory StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Cannot_Load_Error.html")); StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Folder", CreationCollisionOption.OpenIfExists); //we cannot display the original error page, hence copy it to another location await file.CopyAsync(folder, "Cannot_Load_Error.html", NameCollisionOption.ReplaceExisting); //show the error page MainWeb.Navigate(new Uri("ms-appdata:///local/Folder/Cannot_Load_Error.html")); }
//first method to be called upon starting the app private async void Page_Loaded(object sender, RoutedEventArgs e) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed; SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested; //check to see if app is loaded just now if (App.started == true) { try { //if app is loaded just now then try to navigate to homepage MainWeb.Navigate(new Uri((string)AllSettings.Values["HomePage"])); AllSettings.Values["Incognito"] = "Off"; } catch (Exception) { //if try fails, probably this is the first time app is launched and hence we need to set up envionment AllSettings.Values["HomePage"] = " "; AllSettings.Values["Incognito"] = "Off"; } try { var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync(); foreach (var download in downloads) { CancellationTokenSource cts = new CancellationTokenSource(); await download.AttachAsync().AsTask(cts.Token); cts.Cancel(); } } catch (Exception) { } //app is launched just now hence no history or future or present, so disable all buttons Backbtn.IsEnabled = false; Forwardbtn.IsEnabled = false; Stop_Refreshbtn.IsEnabled = false; App.started = false;//give sign that app is now running so that next time, we won't fall here } //if we've got a url from history that needs to launched then check its existence else if (!string.IsNullOrEmpty(App.urlFromOtherWindows)) { MainWeb.Navigate(new Uri(App.urlFromOtherWindows));//and navigate to it App.urlFromOtherWindows = null; } }
//handles home button clicks private void Homebtn_Click(object sender, RoutedEventArgs e) { try//try to navigate to home address { //gets the homepage address from 'HomePage' Settings string HomeURLstring = ((string)AllSettings.Values["HomePage"]); if (HomeURLstring != " ")//check if it is null { Uri HomeURL = new Uri(HomeURLstring); MainWeb.Navigate(HomeURL); } } catch (Exception) { //if try fails, probably this is the first time app is launched and hence we need to set up envionment AllSettings.Values["HomePage"] = " "; } }
//method is called when enter is pressed after typing url private async void URLtb_KeyDown(object sender, KeyRoutedEventArgs e) { //try to resolve the url try { int searchIndex = (int)AllSettings.Values["SearchEngine"]; //accept the final url only if 'enter' key is pressed if (e.Key == Windows.System.VirtualKey.Enter) { string bURL = URLtb.Text.ToString(); string fURL; if (bURL.Contains(".com") == true || bURL.Contains(".") == true) { string extra = "http://"; //if url is already resolved then don't do anything if (bURL.StartsWith("http://") == true || bURL.StartsWith("https://") == true || bURL.Contains("://") == true) { fURL = bURL; } //if url doesn't contain 'www' but ends with '.com' then add 'www.' else if (bURL.StartsWith("www.") == false && bURL.EndsWith(".com") == true) { fURL = extra + "www." + bURL; } //if url is of different type other than containing '.com' else if (bURL.Contains(".") == true) { fURL = extra + bURL; } //if url doesn't contain 'www' and 'com' as well then add both else if (bURL.EndsWith(".com") == false) { fURL = extra + "www." + bURL + ".com"; } //if url contains both then add only 'http://' prefix else { fURL = extra + bURL; } } else if (searchIndex == 0) { fURL = "https://www.google.co.in/?gfe_rd=cr&ei=DXE1WaOLH9eB1ATb3ZXwDQ#q=" + bURL; } else if (searchIndex == 1) { fURL = "https://www.bing.com/search?q=" + bURL; } else if (searchIndex == 2) { fURL = "https://in.search.yahoo.com/search;_ylc=X3oDMTFiaHBhMnJmBF9TAzIwMjM1MzgwNzUEaXRjAzEEc2VjA3NyY2hfcWEEc2xrA3NyY2hhc3Q-?p=" + bURL; } else if (searchIndex == 3) { fURL = "https://duckduckgo.com/?q=" + bURL; } else { string extra = "http://"; //if url is already resolved then don't do anything if (bURL.StartsWith("http://") == true || bURL.StartsWith("https://") == true) { fURL = bURL; } //if url doesn't contain 'www' but ends with '.com' then add 'www.' else if (bURL.StartsWith("www.") == false && bURL.EndsWith(".com") == true) { fURL = extra + "www." + bURL; } //if url is of different type other than containing '.com' else if (bURL.Contains(".") == true) { fURL = extra + bURL; } //if url doesn't contain 'www' and 'com' as well then add both else if (bURL.EndsWith(".com") == false) { fURL = extra + "www." + bURL + ".com"; } //if url contains both then add only 'http://' prefix else { fURL = extra + bURL; } } //finally navigate to url MainWeb.Navigate(new Uri(fURL)); //url textbox shouldn't contain blinking line LoseFocus(sender); } } catch (Exception) { //if url cannot be resolved then dignosis StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Cannot_Load_Error.html")); StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Folder", CreationCollisionOption.OpenIfExists); //we cannot display the original error page, hence copy it to another location await file.CopyAsync(folder, "Cannot_Load_Error.html", NameCollisionOption.ReplaceExisting); //show the error page MainWeb.Navigate(new Uri("ms-appdata:///local/Folder/Cannot_Load_Error.html")); } }