简体   繁体   中英

UWP C# Windows IoT Create Appointment

I have an earlier post UWP C# Windows 10 IoT Alarm Clock which I am currently switching to use Appointment . I have a dialog to add appointment. When I run on rasp pi it doesn't seem to save the appointment & trigger it. Please advise. I also hope to be able to trigger external output pin when the appointment triggers.

对话

private async void Save_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
        var appointment = new Windows.ApplicationModel.Appointments.Appointment();
        var recurrence = new Windows.ApplicationModel.Appointments.AppointmentRecurrence();

        var scheduleTime = TimePicker.Time;
        var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
        var startTime = new DateTimeOffset(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, scheduleTime.Hours, scheduleTime.Minutes, 0, timeZoneOffset);
        appointment.StartTime = startTime;
        appointment.Subject = "Schedule Timer";

        appointment.Duration = TimeSpan.FromMinutes(5);

        if(dailyAlarm.IsOn == true)
        {
            recurrence.Unit = Windows.ApplicationModel.Appointments.AppointmentRecurrenceUnit.Daily;
        } else
        {
            if (setMonday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Monday; }
            if (setTuesday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Tuesday; }
            if (setWednesday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Wednesday; }
            if (setThursday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Thursday; }
            if (setFriday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Friday; }
            if (setSaturdayday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Saturday; }
            if (setSunday.IsChecked == true) { recurrence.DaysOfWeek |= Windows.ApplicationModel.Appointments.AppointmentDaysOfWeek.Sunday; }
        }



        string appointmentId = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default);

        }

约会功能已添加

Not a real answer to your question because I cannot solve that problem, my Pi is at home and I am in the office. However, I believe that you could create something very similar to what that is doing and consume it for your application.

Here is a sample class:

public delegate void AlarmReadyToRing(object sender, object alarm);
public class AppointmentViewer
{
    public event AlarmReadyToRing AlarmIsReady;
    public List<object> Appointments { get; private set; }

    private Timer _AlarmClock;

    public AppointmentViewer()
    {
        LoadAppointmentsFromStorage();
        _AlarmClock = new Timer(TriggerAlarms, null, 0, (int)TimeSpan.FromSeconds(1).TotalMilliseconds);
    }

    private void TriggerAlarms(object state)
    {

        if (DateTime.Now.Second == 59)
        {
            // Reset the timer so that it is checking every 60 seconds
            _AlarmClock.Change(0, (int)TimeSpan.FromSeconds(60).TotalMilliseconds);
        }

        // Find all alarms that should be going off now
        // FindAppointments(x=>x.StartTime == Datetime.Now)
        var readyAlarms = FindAppointments(x=> 1==1);
        foreach (var alarm in readyAlarms)
        {
            AlarmIsReady?.Invoke(this, alarm);
        }
    }

    public void SaveAppointment(object appt)
    {
        // Save appointment logic
        Appointments.Add(appt);
    }

    public void LoadAppointmentsFromStorage()
    {
        // Load appointments from local storage or other
        Appointments = new List<object>();
    }

    public List<object> FindAppointments(Func<object, bool> search)
    {
        var found = Appointments.Where(search);
        return found.ToList();
    }
}


public class SomeOtherClass
{
    private static AppointmentViewer ApptViewer { get; set; } = new AppointmentViewer();

    public SomeOtherClass()
    {
        // Register for event
        ApptViewer.AlarmIsReady += DoSomething;
    }

    private void DoSomething(object sender, object alarm)
    {
        // Here is the incoming alarm that needs to be going off
        // Apply logic for app to display alarm
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM