How to resolve back button issue in windows phone 8 game application? - c#

This question may be possible duplicate of
WP8 back button go two steps back
My game page structure as follow
MainPage(Select New Game) - > Level1 Page(After complete 60 sec this page automatically goto next page->Alert page (it contain TryAgain-Home-Exit Buttons if user click TryAgain Button "Level1" Page Show)
My problem start here..
Now User in "Level1" user click phone back button it show the Alert Page . it's wrong
but correct way is from "Level1" to MainPage
I try following code in Level1 Page
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
RootFrame.RemoveBackEntry();
base.OnBackKeyPress(e);
}
I got error in RootFrame The name 'RootFrame' does not exist in the current context
Any one tell me where i made mistake and what code i need to add for RootFrame. this is my first Wp8 game app development. Thank You

I just had this same problem. The tutorial uses RootFrame.RemoveBackEntry();
The default is NavigationService.RemoveBackEntry();

Related

How can I open page in my Xamarin app without using navigation?

I have a Xamarin Forms application, which has 2 buttons on the toolbar. Currently I'm using FreshMVVM (and it's navigation). I want to load pages without putting them into the navigation. I do not want to have the 'back' button on the toolbar, also I do not want the user return the last page.
Here's how I push a page currently:
CoreMethods.PushPageModel<FirstChoicePageModel>();
I tried to push as a modal, but that way the toolbar buttons does not work until I press back. Should I make new navigation containers and switch to them if I push the buttons?
It's been a little while since I've used FreshMVVM, but if I recall correctly, you have 2 options:
If you want to completly reset the nav stack, you can do CoreMethods.PushPageModelWithNewNavigation<FirstChoicePageModel>();
If you want to keep the nav stack, you can set NavigationPage.SetHasBackButton(this, false); in the code behind of the view.
For both of these options, it may necessary to intercept the back button on Android. In the code behind there is an overrideable method:
protected override bool OnBackButtonPressed()
{
return true; // prevent Xamarin.Forms from processing back button
}
If you want to show a page, but you doesn't want to navigate (change the current page), you can always use Popups.
See this link https://github.com/rotorgames/Rg.Plugins.Popup

Is my code legal in Android usage?

I asked a question recently about how to disable the back button is Android, after a while I got it working with these lines of code
public override void OnBackPressed ()
{
// base.OnBackPressed (); /* Comment this base call to avoid calling Finish() */
// Do nothing
}
And just recently someone commented this
Disabling the back button is counter-intuitive and breaks the device
usage contract imposed by Android. So i suggest you rethink.
-Question-
What would be a possible change to this? I dont want to be able to press the back button when playing my quiz game because that would make be able to cheat. New to android Development
Instead of simply making the back button do nothing, you could have it create a popup asking something along the lines of "Are you sure you want to leave the quiz? (This will count as a loss)". And have it take the user back to the main page of your app if he confirms (instead of back to the previous page).
Why not imitate what many websites do and make it so going 'back' to a page works but doesn't display any information?
It depends on your code, but perhaps you can make your buttons and text (or whatever it is you don't want them interacting with) change to be unseen whenever they move on to a new page. Or just throw up a message that says 'You can't do that' to cover the page that they'll only ever see if they go back to view it again.

Regain focus after flyout closed in a Store App

I have a flyout page which I use to login. I store the username in the localsettings and its all fine up to this point. After the login I want the pagetitle of the page to be something like: "welcome: " + username. (where username is stored in the localsettings).
This works. But only after I reloaded the mainpage by for example going to another page and back again or when I close the entire app and start it again. It doesn't work right away after "login in" do to localsettings only being loaded when a page is started not when its already running.
Is there a way to "reload" the mainpage after the flyout closes? Is there something that I can execute when the mainpage gains focus? Might there be a method for regaining focus(if so I can't find it).
This is (a part of) my flyout page where i store the variables in my localsettings:
localSettings.Values["settingUserName"] = TbUserName.Text;
localSettings.Values["settingPassword"] = TbPassWord.Text;
*note : Frame.Navigate(typeof(page) does NOT work. since flyouts doesn't support that.
I found the solution, ill share it here if anybody else ever needs it.
In C# store apps there is something that checks if the screen got focus. To add this method just add this to the code of the page you want to check if it has focus (or just gained focus).
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
}

C# Windows App remove page

I made a simple Windows 8 quiz game.
I have 3 pages: MainPage, GamePage and ScorePage
The GamePage has a timer so I have to answer as many questions in 30 seconds as I can. It also has a Back button in case I want to give up and a Retry button to start over.
The problem is that whenever I click the Back or Retry button, the new page is displayed correctly but the old GamePage still works in the background and when the time is up it will display the ScorePage
For example I open the app, start the game and click Back. After the time is up, even if I closed the game (or so i thought), the ScorePage will still be displayed...
So how can I completely dispose of that instance of the page?
It is the first time I tried to make a W8 App, I only used WinForms before, where I used this.Close(); to completely get rid of the pages/forms.
To open pages I used this.Frame.Navigate(typeof(GamePage), null); and this.Frame.Navigate(typeof(ScorePage), new int[] { Correct, Wrong });
The below code remove back entries from the stack
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
You can close the app this way
Application.Current.Terminate();

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.

Categories