private static void EnsureScheduled(MyAlarm alarm, bool checkForExisting) { var notifier = ToastNotificationManager.CreateToastNotifier(); IReadOnlyList <ScheduledToastNotification> existing = null; if (checkForExisting) { var tag = GetTag(alarm); existing = notifier.GetScheduledToastNotifications() .Where(i => i.Tag.Equals(tag)) .ToList(); } DateTime now = DateTime.Now; DateTime[] alarmTimes = GetAlarmTimesForAlarm(alarm); foreach (var time in alarmTimes) { if (time.AddSeconds(5) > now) { // If the alarm isn't scheduled already if (!checkForExisting || !existing.Any(i => i.DeliveryTime == time)) { var scheduledNotif = GenerateAlarmNotification(alarm, time); notifier.AddToSchedule(scheduledNotif); } } } }
static void Main(string[] args) { MyAlarm alram = new MyAlarm(AlarmClocks.normalAlarm); // 设定两个延时闹钟 (相当于计时器) alram.SetClockAfter(3); alram.SetClockAfter(10, "Ohhhhhhh"); // 设定一个闹钟, 在固定的时间响起 alram.SetClockAt(System.DateTime.Now.AddSeconds(15), "Wake up! Wake up!"); // 等待所有闹钟响铃之后再结束程序 AlarmClocks.wait(); }
public static void RemoveAlarm(MyAlarm alarm) { var notifier = ToastNotificationManager.CreateToastNotifier(); var tag = GetTag(alarm); // Find all of the scheduled toasts for the alarm var scheduledNotifs = notifier.GetScheduledToastNotifications() .Where(i => i.Tag.Equals(tag)); // Remove all of those from the schedule foreach (var n in scheduledNotifs) { notifier.RemoveFromSchedule(n); } }
private void btnConfirm_Click(object sender, RoutedEventArgs e) { FrameworkElement button = sender as FrameworkElement; MyAlarm data = (MyAlarm)button.DataContext; this.Cursor = Cursors.Wait; Helper.Remote.Invoke <MyAlarm[]>("ConfirmAlarm", (ret, err) => { this.Cursor = null; if (err != null) { MessageBox.Show(this, err); } else { data.IsConfirm = true; } }, data.id); }
private async void ListViewAlarms_ItemClick(object sender, ItemClickEventArgs e) { MyAlarm alarm = e.ClickedItem as MyAlarm; MessageDialog dialog = new MessageDialog("Do you want to delete this alarm?"); dialog.Commands.Add(new UICommand("Delete") { Id = "delete" }); dialog.Commands.Add(new UICommand("Cancel")); var result = await dialog.ShowAsync(); if (object.Equals(result.Id, "delete")) { await DataModel.DeleteAlarm(alarm); } }
private async void ButtonSave_Click(object sender, RoutedEventArgs e) { string name = TextBoxName.Text; TimeSpan time = TimePickerTime.Time; bool onlyOnce = ComboBoxRepeats.SelectedIndex == 0; var alarm = new MyAlarm() { Name = name, TimeOfDay = time }; if (onlyOnce) { if (time < DateTime.Now.TimeOfDay) { // If time for today has already passed, set it for tomorrow alarm.SingleFireTime = DateTime.Today.AddDays(1).Add(time); } else { // Otherwise, set it for today at the time alarm.SingleFireTime = DateTime.Today.Add(time); } } else { alarm.DaysOfWeek = new DayOfWeek[] { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday }; } await DataModel.AddAlarm(alarm); Frame.GoBack(); }
private static ScheduledToastNotification GenerateAlarmNotification(MyAlarm alarm, DateTime alarmTime) { // Using NuGet package Microsoft.Toolkit.Uwp.Notifications ToastContent content = new ToastContent() { Scenario = ToastScenario.Alarm, Visual = new ToastVisual() { BindingGeneric = new ToastBindingGeneric() { Children = { new AdaptiveText() { Text = $"Alarm: {alarm.Name}" }, new AdaptiveText() { Text = alarmTime.ToString() } } } }, Actions = new ToastActionsSnoozeAndDismiss() }; // We can easily enable Universal Dismiss by generating a RemoteId for the alarm that will be // the same on both devices. We'll just use the alarm delivery time. If an alarm on one device // has the same delivery time as an alarm on another device, it'll be dismissed when one of the // alarms is dismissed. string remoteId = (alarmTime.Ticks / 10000000 / 60).ToString(); // Minutes return(new ScheduledToastNotification(content.GetXml(), alarmTime) { Tag = GetTag(alarm), // RemoteId is a 1607 feature, if you support older systems, use ApiInformation to check if property is present RemoteId = remoteId }); }
private static DateTime[] GetAlarmTimesForAlarm(MyAlarm alarm) { if (alarm.IsOneTime()) { return(new DateTime[] { alarm.SingleFireTime }); } else { DateTime today = DateTime.Today; List <DateTime> answer = new List <DateTime>(); for (int i = 0; i < DAYS_IN_ADVANCE_TO_SCHEDULE; i++) { if (alarm.DaysOfWeek.Contains(today.DayOfWeek)) { answer.Add(today.Add(alarm.TimeOfDay)); } today = today.AddDays(1); } return(answer.ToArray()); } }
private static string GetTag(MyAlarm alarm) { // Tag needs to be 16 chars or less, so hash the Id return(alarm.Id.GetHashCode().ToString()); }
public static void EnsureScheduled(MyAlarm alarm) { EnsureScheduled(alarm, checkForExisting: true); }
public static void ScheduleAlarm(MyAlarm alarm) { EnsureScheduled(alarm, checkForExisting: false); }