C# Windows App remove page - c#

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();

Related

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.

How to correctly pause/delay Windows Forms application

I am a beginner to the OOP and the C#.
I am working on a quiz game using the Windows Forms.
My problem is related to two classes, the form and the game logic.
I have a basic UI with classic Froms controls. Take a look.
The thing I want to achieve is, when a player presses any answer button, it will higlight that pressed button by red or green color, depending on if it is right or wrong answer. After changing the color I want the program to wait for a while and then go to the next question.
Probelm is, that I don´t know how to achieve this correctly. I don´t know how to work with threads and how exactly the Form app works related to threads. Should I use a thread sleep or a timer or a async?
I will show you the method in game logic class which should handle this.
public static void Play(char answer) //Method gets a char representing a palyer answer
{
if (_rightAnswer == answer) //If the answer is true, the button should become green
{
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.LightGreen);
_score++;
}
else //Otherwise the button becomes Red
{
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.Red);
}
//SLEEP HERE
if (!(_currentIndex < _maxIndex)) //If it is the last question, show game over
{
Program.MainWindow.DisplayGameOver(_score);
}
else //If it is not the last question, load next question and dispaly it and finally change the button color to default
{
_currentIndex++;
_currentQuestion = Database.ListOfQuestions.ElementAt(_currentIndex);
_rightAnswer = _currentQuestion.RightAnswer;
Program.MainWindow.DisplayStats(_score, _currentIndex + 1, _maxIndex + 1);
Program.MainWindow.DisplayQuestion(_currentQuestion.Text);
Program.MainWindow.DisplayChoices(_currentQuestion.Choices);
}
Program.MainWindow.ChangeBtnColor(answer, System.Drawing.SystemColors.ControlLight);
}
I don´t want to completely block the UI but also I don´t want users to make other events by pressing other buttons during the pause. Because it will result in improper run of app.
If the program is really simple and you do not want to implement Threads I would suggest using Timer. Just start your Timer when clicking answer button. Your timer should contain function which would stop itself after some time and do other actions needed (e.g. pick another question).
Once the user has selected an answer you can disable all the buttons so they can't press anything else.
Then start a timer so you don't block the UI. The timer is basically a thread but handles all the threading for you so you don't have to worry about that aspect.
When the timer reaches the desired delay stop it and fire an event to select the next question.
At //SLEEP HERE add this line of code
Timer timer = new Timer(new TimerCallback(timerCb), null, 2000, 0);
The 2000 is milliseconds and is the wait time, timerCb is a call back method.
Also under that disable all your buttons so new events wont be generated.
private void timerCb(object state)
{
Dispatcher.Invoke(() =>
{
label1.Content = "Foo!";
});
}
You can do whatever you want in the callback, however if you do something that would change anything in the UI, you need to use the Dispatcher like I have changing the label content.
Suspending execution in a GUI scenario is very easy thanks to await:
await Task.Delay(2000);
This does not block the UI.
You should research what await does and how to use it. If you have never heard about it and are programming WinForms you are doing something wrong.
No timers or threads are needed. No callbacks, no Invoke.

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

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();

Capture and save multiple images with `CameraCaptureDialog`

I have a Windows Mobile (Compact framework 2) application that defines a user control MPhotoControl. MPhotoControl shows a default image and when the user clicks on this image a CameraCaptureDialog is opened to allow a photo to be captured. Once captured, the photo is then displayed in the user control. This works fine for capturing a single photo and then going back to the application.
The problem is that when there are lots of these controls on a particular form then the user interface becomes very unfriendly because the user has to show the camera dialog, take a photo, save and close the dialog for every photo control on the form. What the users are asking for is a mechanism to open the CameraCaptureDialog, take several photos without the dialog closing until all the photo controls have images.
I am trying to implement this, but I don't see a way to get the CameraCaptureDialog to capture and save several photos at once. As far as I can tell it is not possible, because when the dialog shows on my HTC Touch Diamond, I only have the options to "Accept the photo" (arrow icon), "Capture again" (camera icon) or "cancel and close dialog" (dustbin icon). And when I click the arrow to accept it always closes the dialog box.
So does anyone know of a way of capturing and saving more than one image at a time using CameraCaptureDialog?
I then thought of trying to open the CameraCaptureDialog multiple times as a work around. So as soon as the first image is saved the dialog is immediately opened again to capture the second image. Here is my code showing my attempt at a workaround:
public partial class MPhotoControl : UserControl
{
public static IEnumerable<MPhotoControl> PhotoControls;
...
private void CaptureMultiplePhotos()
{
foreach (MPhotoControl photo in PhotoControls)
{
using (CameraCaptureDialog cameraDialog = new CameraCaptureDialog())
{
if (cameraDialog.ShowDialog() != DialogResult.OK)
{
break;
}
photo.CapturePhoto(cameraDialog.FileName);
}
}
}
}
The problem with this is that the CameraCaptureDialog still only opens once and the subsequent call to the ShowDialog method simply returns DialogResult.Cancel. So, does anyone know why this workaround does not work and if it is possible to get the dialog to immediately re-open once the previous captured image has been saved?
Please look here: http://www.hjgode.de/wp/2012/10/17/windows-mobile-cameracapturedialog-alternative/
I am unable to attach any code or binaries here, so I did a new blog post.
The code start the camera app, waits for its close-down and presents you with a list of new photos.
Code is not yet perfect but a starting point.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Categories