简体   繁体   English

在Xamarin C#中显示Google Calendar API v3事件dateTime

[英]Displaying Google Calendar API v3 Events dateTime in Xamarin C#

I'm trying to take events from Google Calendar and display them in a listView for Xamarin.Forms. 我正在尝试从Google日历中获取事件,并将其显示在Xamarin.Forms的listView中。

Google Calendar uses an RFC3339 format when you call start.dateTime the format is yyyy-mm-dd. 调用start.dateTime时,Google日历使用RFC3339格式,格式为yyyy-mm-dd。 I'm trying to do a string day of the week format (Mon Jan 1, 2018) similar to .Net DateTime.ToLongDateString Method. 我正在尝试使用类似于.Net DateTime.ToLongDateString方法的星期几格式的字符串(2018年1月1日星期一)。

My Code so far is this: 到目前为止,我的代码是:

namespace TheFirstAcademy.ViewModels{
class EventListViewModel
{
    public List<SchoolEvent> SchoolEvents { get; set; }
    public SchoolCalendar SelectedCalendar { get; set; }


    public EventListViewModel(SchoolCalendar selectedcalendar)
    {
        SelectedCalendar = selectedcalendar;
        SchoolEvents = GetSchoolEvents();

    }

    public List<SchoolEvent> GetSchoolEvents()
    {
        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            ApiKey = "Key",
            ApplicationName = "TFA Calendar Mobile App",
        });

        // Define parameters of request.
        EventsResource.ListRequest request = service.Events.List(SelectedCalendar.SchoolCalId);
        request.TimeMin = DateTime.Now;
        request.ShowDeleted = false;
        request.SingleEvents = true;
        request.MaxResults = 10;
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

        // List events.
        Events events = request.Execute();
        List<SchoolEvent> schoolEvents = new List<SchoolEvent>();

        if (events.Items != null && events.Items.Count > 0)
        {
            foreach (var eventItem in events.Items)
            {
                string when = eventItem.Start.DateTime.ToString();
                if (String.IsNullOrEmpty(when))
                {
                    when = eventItem.Start.Date;
                }

                schoolEvents.Add(new SchoolEvent() {
                    EventTitle = eventItem.Summary,
                    EventDescription = eventItem.Description,
                    EventStartTime = when,
                    EventEndTime = eventItem.End.Date
                });
            }
        }
        return schoolEvents;
    }
}

} }

This will eventually get displayed in a ListView for iOS and Android using Xamarin.Forms. 最终将使用Xamarin.Forms在iOS和Android的ListView中显示此内容。 The Event for a calendar would look something like this: 日历的事件如下所示:

[Event Name] [活动名称]

[Event Location] [活动地点]

Starts: Fri May 25, 2018 12:00 PM 开始于:2018年5月25日星期五12:00 PM

Ends: Sat May 26, 2018 12:00 PM 结束于:2018年5月26日星期六12:00 PM

Some references I have been found: Google Calendar v3 Events info https://developers.google.com/calendar/v3/reference/events 我发现了一些参考:Google Calendar v3活动信息https://developers.google.com/calendar/v3/reference/events

RFC to dateTime (not sure if this is the right direction). RFC到dateTime(不确定这是正确的方向)。 How do I parse and convert a DateTime to the RFC 3339 date-time format? 如何解析DateTime并将其转换为RFC 3339日期时间格式?

Any help is appreciated. 任何帮助表示赞赏。

I figured out the conversion by using. 我通过使用找出了转换。 DateTime.Parse() DateTime.Parse()

I converted the eventItem.Start.Date (which is a string)to DateTime using DateTime.Parse(). 我使用DateTime.Parse()将eventItem.Start.Date(它是一个字符串)转换为DateTime。 Then I converted the DateTime to string ("ddd MMM dd, yyyy"). 然后,我将DateTime转换为字符串(“ ddd MMM dd,yyyy”)。

Probably basic stuff Maybe this will help someone. 可能是基本的东西,也许这会对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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