Kill an app when return from WebBrowseTask - c#

I made a small app, and I have a button than pin to start a web blog via a xaml, so when the tile is pin, it goes to the xaml, which automatically redirect to the blog by WebBrowserTask. MY problem is that if they press the back button from the web page, they go to the xaml, and I want them to go directly to the start menu, so I'm looking for a way to kill the app when they return from the
the xaml code is
private void WebPages_Loaded(object sender, RoutedEventArgs e)
{
try
{
WebBrowserTask task = new WebBrowserTask();
task.Uri = new Uri(NavigationContext.QueryString["uri"]);
task.Show();
}
catch (KeyNotFoundException)
{
MessageBox.Show("No URI was passed to the app.");
}
}
I don't know if this is possible, but if someone knows i'll be pleased. Thanks

Try this : Windows Phone 7 close application to kill an app, you may want to try this solution :
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}

You may want to load your blog inside a WebBrowser control rather than running a WebBrowseTask. Then, clear the back stack if need be in the OnBackKeyPress event. If you open a WebBrowserTask, the user will have to hit back to resume your app and then hit back again to return to the start menu. You won't be able to exit your app automatically after resuming. Its against the marketplace guidelines.

Related

Is there a method like Android's Finish() in Xamarin Forms?

I am looking for a method that will end the previous pages/activities as I navigate through different pages so that when the back button is pressed it does not navigate back to that previous message. In Java Android such a method is called finish(), so is there such a method in Xamarin Cross Platform?
Xamarin Forms has some options to remove from or add pages to the stack. Take a look at Navigation.PopAsync or Navigation.RemovePage.
On Xamarin Form for Android, you have a similar method for that: Finish();
I think you are finding this method?
There's no method finish in xamarin forms because finish() is to let the system know that the programmer wants the current Activity to be finished. In xamarin forms you only have one Activity which is MainActivity. Xamarin forms revolves around Navigation, So if you want to go back to previous navigation Navigation.PopAsync or if you want to close a modal use Navigation.PopModal
Based on how you described your objective, I think this.Finish(); will work like a charm.
Example Code:
Button nextPage;
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.your_content_xml);
// Set our view from the "main" layout resource
nextPage = FindViewById<Button>(Resource.Id.button1);
nextPage.Click += (s, e) =>
{
Intent nextActivity = new Intent(this, typeof(next_page_CS_file)); // Create Intent instance
StartActivity(nextActivity); // Go to the Next Page as set in Intent
this.Finish(); // Terminates Current Page.
// As such, once you're in the next page, clicking the back button
// on phone's menu will close the App instead of going back here
};
} catch (Exception ex)
{
// Any code you want that'll handle the Exception.
// Like, for example, . . .
Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
}
}
From my understanding and experience, this should work. However I'm unsure if there are other ways of accomplishing what you aim for.

Forms Web Browser control won't proceed or redirect on it's own

I have a main form that opens a new form that contains a web browser control that navigates to the entered url when it is loaded. I've tried different things I've found and this is my latest code: I have a function (GoToURL) that is triggered at the Shown event of the form which is here:
public delegate void Launch();
private void launchBrowser()
{
webBrowser1.Navigate(GlobalData.URL);
}
private void GoToURL(object sender, EventArgs e)
{
webBrowser1.Invoke(new Launch(launchBrowser));
}
I have nothing in the Document Completed function:
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
The web browser control loads the url just fine and I can scroll around in it, but on some links or buttons the control goes white and nothing happens. It won't proceed with the desired function. A more specific example is I am logging into a site and once I click the submit button it just hangs there. A "loading" image is presented and it just sits there. I know the Document Completed function above is triggered when this happens, too.
I apologize for my inexperience with C# and this is the first time using the web browser control (forms not wpf), so I am at a loss of what to try. I suspect its a threading issue, but that's as far as I got.
You are experiencing issues within the browser itself. Please launch IE and test the same there. I suspect you'll see the same issues.
Your experience is tied to the version of Internet Explorer you have in your system, and javascript errors have been frequents in the earlier versions of IE. Upgrade to IE11 in any case.
If that's not an option, try using a browser control with a different rendering engine. I'm too lazy to find and list the options, but I've used a few and they work well.
to avoid JavaScript Errors please follow this
public frmMain()
{
//Java Script Error popup block
webBrowser1.ScriptErrorsSuppressed = true;
}
Please Check this on loading Uri using
webBrowser1.Url = new Uri(Url.Trim().ToString());
Url - your desired Url

Windows Phone 8 NavigationService.Navigate throws a NullReferenceException

I am working on a C# Project for WindowsPhone 8. When the user deactivates the App (i.e. Going to the main menu) I stop some timers and save some things. This works fine.
But when the user reactivates the app I want to navigate the user to a pause screen.
NavigationService.Navigate(new Uri("/Pause.xaml", UriKind.Relative));
The Code is simple and works perfect when the app isn't deactivated. But when the user reactivates the app it throws a NullReferenceException.
I already tried several things such as:
public void pause_for_activated()
{
this.Loaded += navigate_pause();
}
private RoutedEventHandler navigate_pause()
{
NavigationService.Navigate(new Uri("/Pause.xaml", UriKind.Relative));
return navigate_pause();
}
When I start the app without automatically navigating to the pause screen and then press a button to navigate it works.
My Question is now when can I automatically navigate and how can I do this ?
In advance , I thank you already.
I got no answer until now, but I found a solution for me so I decided to put it here.
Loaded += navigate_pause();
private RoutedEventHandler navigate_pause()
{
Dispatcher.BeginInvoke(() =>
{
((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(new Uri("/Pause.xaml", UriKind.Relative));
});
return null;
}

Hide elements in a web page whilst using a web-browser control for WP7

I am currently working on an app for WP7 for my university, and need a temporary solution to a problem. Now this solution is, that I will be loading a webpage using the web browser control for WP7. For example: http://m.iastate.edu/laundry/
Now as you see on the webpage, there are certain elements I want to hide, for example the back button. For now, what I have done to handle the back button is something like this:
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
// Handle loading animations
// Handle what happens when the "back" button is pressed
Uri home = new Uri("http://m.iastate.edu/");
// The the current loading address is home
// Cancel the navigation, and go back to the
// apps home page.
if (e.Uri.Equals(home))
{
e.Cancel = true;
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
Now that works beautifully, except for the part that there is a back button on the hardware.
So my second option is to completely hide the back button ONLY on that page, and not its children. So not on http://m.iastate.edu/laundry/l/0
I am still debating on just parsing the data and displaying it in my own style, but I'm not sure if that's completely needed seeing how the data needs constant internet service and is already in a well-put format. Plus, I feel like that would be a waste of resources? Throw in your opinions on that too :)
Thanks!
You should inject a script in the page with InvokeScript.
Here is the kind of Javascript code you need to remove the back button:
// get the first child element of the header
var backButton = document.getElementsByTagName("header")[0].firstChild;
// check if it looks like a back button
if(backButton && backButton.innerText == "Back") {
// it looks like a back button, remove it
document.getElementsByTagName("header")[0].removeChild[backButton];
}
Call this script with InvokeScript:
webBrowser1.InvokeScript("eval", "(function() { "+ script +"}()");
Warning: IsScriptEnabled must be set to true on the web control
If the removal of the back button depends of the page, just test the navigating URI in C# and inject the script if neeeded.

Decide which page to go when launching in Windows Phone 7.1 app

I am building an app for Windows Phone 7.1 using c#.
when the app is opening, if the user is the first time using it, then go to the "set up a password" page, else go to the "login page".
I wanted to use NavigationService.Navigate(Uri), but I don't know where should i call this function?
I suggest to save the password to some sort of (encrypted) persistent storage, and try to retrieve it when the app starts.
Then add this code in the App.xaml and it should do the trick
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
// Only care about MainPage
if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
return;
var password = GetPasswordFromSomePersistentStorage();
// Cancel current navigation and schedule the real navigation for the next tick
// (we can't navigate immediately as that will fail; no overlapping navigations
// are allowed)
e.Cancel = true;
RootFrame.Dispatcher.BeginInvoke(delegate
{
if (string.IsNullOrWhiteSpace(password))
RootFrame.Navigate(new Uri("/InputPassword.xaml", UriKind.Relative));
else
RootFrame.Navigate(new Uri("/ApplicationHome.xaml", UriKind.Relative));
});
}
Make sure you have added the handler on the App() constructor
RootFrame.Navigating += new NavigatingCancelEventHandler(RootFrame_Navigating);
And also, the MainPage.xaml, is just and empty page (set it up as you begin page) that will be used to catch the initial navigation event.
Hope its helps.

Categories