Search code examples
windows-phone-7windows-phone-7.1alarmscheduledactionservice

How to update an existing Alarm in ScheduledActionService on Windows Phone?


In my app, I have an Alarm which is scheduled to start at 8:00am (BeginTime). The problem I am facing is that I am not sure what's the most appropriate way to update its BeginTime.

I tried to locate the Alarm in the ScheduledActionService and then updated it from there. Unfortunately it didn't seem to work, the Alarm never gets triggered.

        var schedule = ScheduledActionService.Find(alarmModel.Name);

        if (schedule != null)
        {
            var alarm = (Alarm)schedule;

            if (alarm.BeginTime != alarmModel.BeginTime)
            {
                alarm.BeginTime = alarmModel.BeginTime;
            }
        }

If I removed it from the ScheduledActionService and then add a new one then it worked.

But this looks ugly though... Is this the only way I can 'update' an Alarm???!

Thanks,

Xin


Solution

  • No. The correct approach must be to call Replace() .

       var schedule = ScheduledActionService.Find(alarmModel.Name);
    
        if (schedule != null)
        {
            var alarm = (Alarm)schedule;
    
            if (alarm.BeginTime != alarmModel.BeginTime)
            {
                alarm.BeginTime = alarmModel.BeginTime;
                ScheduledActionService.Replace(alarm);
            }
        }