添加多个提醒 - Windows手机

时间:2014-03-06 08:37:39

标签: c# windows-phone-7 windows-phone-8 reminders

我已经了解了如何通过此网站创建提醒应用程序http://www.c-sharpcorner.com/uploadfile/f397b9/reminder-application-in-windows-phone-mango/ 但是从这里它只允许添加一个提醒。当我做一个新的提醒时,前一个将被覆盖。如何使它能够接受多个提醒。以下是我的代码:

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 个答案:

答案 0 :(得分: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);
    }

然后使用唯一名称调用方法如果您不希望覆盖现有提醒,例如使用Task1,Task2

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

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