2014-03-06 39 views
1

我已經學會了如何從本網站創建提醒應用http://www.c-sharpcorner.com/uploadfile/f397b9/reminder-application-in-windows-phone-mango/ 但是從這裏它允許只添加一個提醒。當我提出新的提醒時,以前會被忽略。我如何使它能夠接受多個提醒。下面是我的代碼:添加多個提醒 - Windows手機

void btnSave_Click(object sender, RoutedEventArgs e) 
    { 
     DateTime _Date = rDate.Value.Value; 
     TimeSpan _Time = rTime.Value.Value.TimeOfDay; 
     _Date = _Date.Date + _Time; 
     String _Content = titleTBox.Text; 
     if (_Date < DateTime.Now) 
      MessageBox.Show("Your time is not match !\nPlease Enter again !"); 
     else if (String.IsNullOrEmpty(_Content)) 
      MessageBox.Show("Your task can't be empty !\n Please enter to do task !"); 
     else 
     { 

      ScheduledAction _OldReminder = ScheduledActionService.Find("TodoReminder"); if (_OldReminder != null) 
       ScheduledActionService.Remove(_OldReminder.Name); 
      Reminder _Reminder = new Reminder("TodoReminder") 
      { 
       BeginTime = _Date, 
       Title = "Reminder", 
       Content = _Content, 
      }; 
      ScheduledActionService.Add(_Reminder); 
      //MessageBox.Show("Set Reminder Completed"); 
     } 
    } 

回答

1

代碼工作正常,你發現以前註冊的提醒,當它存在,你刪除它,然後註冊一個新的具有相同的名稱。

你的代碼分離到一個簡單的方法

private void RegisterScheduleIfNotExist(string name, string title, string content, DateTime time) 
    { 
     ScheduledAction currentReminder = ScheduledActionService.Find(name); 

     if (currentReminder != null) 
     { 
      ScheduledActionService.Remove(currentReminder.Name); 
     } 

     var reminder = new Reminder(name) 
     { 
      BeginTime = time, 
      Title = title, 
      Content = content, 
     }; 

     ScheduledActionService.Add(reminder); 
    } 

然後調用名唯一的方法,如果您不希望覆蓋現有的提醒,egwith任務1,任務2

RegisterScheduleIfNotExist("Task1", "Task 1 title", "Task 1 content", DateTime.Now.AddMinutes(3)); 

RegisterScheduleIfNotExist("Task2", "Task 2 title", "Task 2 content", DateTime.Now.AddMinutes(5)); 
+0

您好,我已經測試過了!它工作得很好!非常感謝你的解決方案。 –