Ical.net从.net和C#中的日历中查看事件数据

时间:2018-10-23 13:55:27

标签: c#

我已经设置了一个控制台应用程序来测试Ical.net。我有这段代码可以从日历中提取这些事件的发生(效果很好);但我希望能够查看这些事件对象中存在的其他详细信息;而且我没有在文档中看到如何查看详细信息。

这是我的代码:

var calendar = new Calendar();
var recurrenceRule = new RecurrencePattern("FREQ=DAILY");

var vEvent = new CalendarEvent
{
    Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
    End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
    Description = "Test Description on event object",
    Name = "Event Title 1",
    RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);

recurrenceRule = new RecurrencePattern("FREQ=DAILY;INTERVAL=4;WKST=SU");
vEvent = new CalendarEvent
{
    Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
    End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
    Description = "Test2 event description yo!",
    Name = "Event Title 2",
    RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);

var searchStart = DateTime.Parse("2018-10-01");
var searchEnd = DateTime.Parse("2018-10-31");
var occurrences = calendar.GetOccurrences(searchStart, searchEnd);

foreach (var item in occurrences)
{
    Console.WriteLine(item.ToString());
}

Console.ReadLine();      

已更新

通过在Console.ReadLine();

之前添加以下内容,我可以查看一些活动详细信息

代码添加:

foreach (var item in calendar.Children)
{
    Console.WriteLine(item.Name);
}

我现在唯一的问题是如何将发生日期,此item.name和事件描述捆绑在一起,成为一个相互引用的对象/事物?

完整代码:

    var calendar = new Calendar();
var recurrenceRule = new RecurrencePattern("FREQ=DAILY");

var vEvent = new CalendarEvent
{
    Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
    End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
    Description = "Test Description on event object",
    Name = "Event Title 1",
    RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);

recurrenceRule = new RecurrencePattern("FREQ=DAILY;INTERVAL=4;WKST=SU");
vEvent = new CalendarEvent
{
    Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
    End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
    Description = "Test2 event description yo!",
    Name = "Event Title 2",
    RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);

var searchStart = DateTime.Parse("2018-10-01");
var searchEnd = DateTime.Parse("2018-10-31");
var occurrences = calendar.GetOccurrences(searchStart, searchEnd);

foreach (var item in occurrences)
{
    Console.WriteLine(item.ToString());
}

foreach (var item in calendar.Children)
{
    Console.WriteLine(item.Name);
    //result: Event Title 1, Event Title 2
    //Does not have access to even description, start, or end values. 
}

Console.ReadLine();    

1 个答案:

答案 0 :(得分:1)

Children返回ICalendarObject的列表。

如果要检索事件详细信息,则必须回退到CalendarEvent

foreach (ICalendarObject item in calendar.Children)
{
    Console.WriteLine(item.Name);

    if (item is CalendarEvent calendarEvent)
    {
        Console.WriteLine(calendarEvent.Description);
        Console.WriteLine(calendarEvent.Start);
        Console.WriteLine(calendarEvent.End);
    }
}

输出:

  

事件标题2
  Test2事件描述哟!
  01/10/2018 07:00:00
  01/10/2018 08:00:00

要从事件中获取源事件,可以执行以下操作:

foreach (var item in occurrences)
{
    var sourceEvent = item.Source as CalendarEvent;
    Console.WriteLine(sourceEvent.Description);
    Console.WriteLine(sourceEvent.Start);
    Console.WriteLine(sourceEvent.End);
}