【问题标题】:UWP C# Windows IoT Create AppointmentUWP C# Windows IoT 创建约会
【发布时间】:2018-05-31 10:39:38
【问题描述】:

我有一个较早的帖子UWP C# Windows 10 IoT Alarm Clock,我目前正在改用Appointment。 我有一个添加约会的对话框。当我在 rasp pi 上运行时,它似乎没有保存约会并触发它。 请指教。 我也希望能够在约会触发时触发外部输出引脚。

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);

        }

【问题讨论】:

  • 你提到在树莓派上运行时,无法保存约会并触发。这意味着当在其他硬件上运行时,它确实保存约会并触发它。是这样吗?另外,您是否具有Package.appxmanifest 中指定的“约会”功能?
  • 嗨@Naikrovek 我添加了“约会”功能。我设置了约会,它没有触发。我不确定我是否做得对。我有点迷路了。
  • 我记错了,但我相信约会保存与 Microsoft 帐户相关联?我会进一步调查。
  • @mylim 当您说保存在另一台计算机上工作时,您是指您正在调试的本地计算机吗?我认为物联网核心可能不包含存储日历约会的正确系统,因为该数据实际上已同步到用户的帐户。由于 Windows 上的 UWP 存在命名空间,但我找不到建议您不能将此命名空间与 Iot Core 一起使用的文档。也许在他们的 github 上发布一个问题,github.com/MicrosoftDocs/windows-iotcore-docs
  • 如果约会不起作用,为什么不创建一个更简单的数据结构并将该信息以 JSON 格式存储在可以轻松再次加载的文件中?

标签: c# uwp raspberry-pi3 windows-10-iot-core appointment


【解决方案1】:

不是您问题的真正答案,因为我无法解决这个问题,我的 Pi 在家,我在办公室。但是,我相信您可以创建与正在执行的操作非常相似的内容并将其用于您的应用程序。

这是一个示例类:

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
    }
}

【讨论】:

  • 谢谢。我会试试看。顺便问一下,微软是否有他们的 Windows 10 时钟和闹钟应用程序的参考代码?或者有什么办法可以得到它们?
  • @mylim,An Appointment 代表日历中的约会,所以需要日历应用,但 windows IoT Core 不包含日历应用。请参考Manage appiontments,适用于 windows 10。
  • 嗨@MichaelXu-MSFT 为了让我为物联网核心制作一个时间表警报应用程序,最好推荐什么?
  • @mylim,我认为 Bailey Miller 的建议是满足您需求的简单有效的解决方案。我也希望会有更多的讨论。
  • 我的示例类需要进一步充实才能正常工作,但如果你想编写这个警报应用程序,我认为完全控制警报的存储方式和存在的信息会更好你。
猜你喜欢
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多