3

I'm trying to write an alarm clock app for Windows Phone that requires the user to solve a math problem upon the ringing of the alarm. Right now I'm stuck, I've got the MainPage containing the settings for turning on the alarm, and when the alarm rings the user is redirected to another xaml page, one that requires the user to enter the answer to a randomly generated math problem in order to turn off the alarm. My problem is, once the user solves the problem and taps the check box, the app is supposed to first set the alarmSet value to false, and redirect the user back to the mainPage:

private void Solve_Click(object sender, EventArgs e)
    {

        this.userSolve = Convert.ToInt32(answerInput.Text);
        if (userSolve != answer)
        {
            MessageBox.Show("Incorrect");
            //userAnswerInt = Convert.ToInt32(answerInput.Text);


        }

        else if (userSolve == answer)
        {
            MainPage.alarmSet.Value = false;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }


    }

And then when the user arrives at the mainPage, the OnLoaded method is supposed to stop the alarm sound and reset everything back to normal:

public async void OnLoaded(object sender, EventArgs e)
    {

        this.timer.Stop();
        this.alarmSound.Stop();


        alarmSet.Value = false;
        this.notificationSwitch.IsChecked = alarmSet.Value;

        this.timePicker.Value = new DateTime(1, 1, 1,
                                             alarmTime.Value.Hours,
                                             alarmTime.Value.Minutes,
                                             0
                                             );



        if (alarmSet.Value == true)
            this.alarmTimeText.Text = alarmTimeString;
        else if (alarmSet.Value == false)
            this.alarmTimeText.Text = "alarm off";

But the problem is, every time the user solves the math problem, they're taken back to the main page and immediately redirected back to the alarm ringing page, because the alarm is still turned on. This creates an infinite loop of alarms overlapping each other, and I can't figure out why, even though I set alarmSet.Value to false before navigating back to the main page, the alarm is still seen as on... is there something more I need to be doing? How can I have the alarm on and set to the current minute the clock is at without it ringing immediately?

1 Answers1

2

I do not see an "OnLoaded" event on the Application life cycle for Windows Phone.

Refer: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff817008(v=vs.105).aspx

You may do the following:

  1. In Mainpage.xaml, see if the alarm is running and if the alarmSet is true, redirect user to next page under onNavigatedTo Event.

  2. In next page, set alarmSet as false and redirect user to MainPage.xaml. The onNavigatedTo will again be fired, however, since alarmSet is false, user wont be redirected anywhere. You may do the else case of stopping alarm or anything further here.

SreekanthGS
  • 988
  • 5
  • 12
  • Thanks for the response, I'm basing this app off of the "alarm clock with voice commands" sample from the Windows Phone code samples page. The OnLoaded event was already existing in the code. At first I put all my code in the OnNavigatedTo event, but after debugging it seems that upon redirection back to the mainpage, the OnLoaded event is run instead of the OnNavigatedTo event. I already have the alarmSet.Value being set to false before navigating back to the mainpage, and for some reason the alarm is still seen as 'on' and basically re-triggers the alarm. – Conner Owen Apr 22 '14 at 04:48
  • So for some reason, it seems that upon navigation from the alarm ringing page back to the mainpage once the user has solved the problem, the mainpage is redrawn completely, recreating everything as if the program was being launched for the first time, rather than just bringing it back into the foreground. How do I just navigate to a new page and keep the mainpage running in the background? – Conner Owen Apr 22 '14 at 04:56
  • Referring to the sample at http://code.msdn.microsoft.com/wpapps/Alarm-Clock-with-voice-7b749124/sourcecode?fileId=70597&pathId=1202898007, the onLoaded event is called only during the first time app launch. See: `// Register the voice commands. Called when the app is first launched. public async void OnLoaded(object sender, EventArgs e)` – SreekanthGS Apr 22 '14 at 04:56
  • It would seem like it's supposed to do that, but for example, I added in the onloaded code to set my timepicker value to the current time plus one hour, and every time I tap on the timepicker to change the time, it takes me back to the mainpage with whatever value I entered plus one hour. The onloaded gets run every time, if I just discard the entire onloaded method will I be okay? I assume this line routes whatever happens on load to the onloaded method ' this.onLoadedEventHandler = new RoutedEventHandler(OnLoaded); this.Loaded += onLoadedEventHandler;' – Conner Owen Apr 22 '14 at 05:17
  • So if I remove the onloaded method, will the app just default to using onNavigatedTo instead? – Conner Owen Apr 22 '14 at 05:18
  • Use the onLoaded for one time use, ie, when the App first gets loaded. It doesnt get called on to and fro navigations. Use the onNavigatedTo for such cases. – SreekanthGS Apr 22 '14 at 05:28
  • OK, I've gotten that sorted out, I just removed the onloaded entirely and everything seems to be working perfectly. The only issue I have now is that when you first turn on the alarm, it rings immediately, because the timepicker's default value is the current time on the device. How can I set it to something like the current time +1 minute or something other than the current time as default? – Conner Owen Apr 22 '14 at 05:37
  • I didn't get your question here. Can you show me the sample code? – SreekanthGS Apr 22 '14 at 05:41
  • I don't really know what part of the code is setting it, but right when I first run my app, the second I turn the alarm on, it rings and takes me to the alarm ringing page, because at the beginning the timePicker is set by default to the current time. There must be some code to change the default value of timePicker, but I'm not sure how. – Conner Owen Apr 22 '14 at 08:33
  • Okay so I fixed that issue by just setting the timePicker value to DateTime.Now.Hour + 1 at the beginning, but I still have the issue where if you set the alarm to the current time, it'll ring immediately, is there any way to do some sort of check to see if the alarm is being set to the current time and not go off? – Conner Owen Apr 22 '14 at 08:42