我正在使用xamarin表单将事件添加到android平台的本机日历中。我做了依赖服务,以跨平台添加事件。我可以添加一个事件,但是在添加任何事件之前,我想应用检查来限制是否存在相同唯一标识符的任何事件,那么它不应允许添加该事件。
我尝试了一下,但是没有用,因为我Cursor
在android平台上的xamarin中找不到任何类。
在Android上,您需要执行以下操作才能使用新的API从日历中获取数据。
您需要许可 android.permisson.READ_CALENDAR
通过使用CalendarContract
类与日历数据进行交互。此类提供了一种应用程序与日历提供程序进行交互时可以使用的数据模型。
枚举已在日历应用程序中注册的日历。为此,我们可以调用ManagedQuery方法。至少,我们需要指定日历的内容Uri和要返回的列;此列规范称为投影。通过调用ManagedQuery
,我们可以查询内容提供者的数据,例如日历提供者,并返回带有查询结果的Cursor。
var calendarsUri = CalendarContract.Calendars.ContentUri;
指定投影:
string[] calendarsProjection = {
CalendarContract.Calendars.InterfaceConsts.Id,
CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,
CalendarContract.Calendars.InterfaceConsts.AccountName,
, CalendarContract.Events.InterfaceConsts.Title
, CalendarContract.Events.InterfaceConsts.Dtstart
, CalendarContract.Events.InterfaceConsts.Dtend
};
您可以传递更多的参数,而不是null。在此处查看其他可用参数。
var cursor = ManagedQuery (calendarsUri, calendarsProjection, null, null, null);
据说托管查询已弃用,最好使用ContentResolver
。
var cursor = context.ContentResolver.Query(calendarsUri, calendarsProjection, null, null, null);
带有日期过滤器:
var selection = "((dtstart <= ?) AND (dtend >= ?))";
var selectionArgs = new string[] { startString, endString };
Forms.Context.ApplicationContext.ContentResolver.Query(calendarsUri, calendarsProjection, selection, selectionArgs, null);
或者
var ctx = Forms.Context;
var cursor = ctx.ApplicationContext.ContentResolver.Query(calendarsUri, calendarsProjection, null, null, null);
要查询的参数是:
此处提供了整个教程和分步说明。在ContentResolver
这里阅读有关内容。
对于iOS,您必须使用EventKit。
要通过事件的ID检索事件,请使用上的EventFromIdentifier
方法,EventStore
并将EventIdentifier
从事件中提取的事件传递给它:
var mySavedEvent = App.Current.EventStore.EventFromIdentifier (newEvent.EventIdentifier);
要搜索日历事件,您必须NSPredicate
通过上的PredicateForEvents
方法创建一个对象EventStore
。AnNSPredicate
是iOS用来查找匹配项的查询数据对象:
NSPredicate query = App.Current.EventStore.PredicateForEvents (startDate, endDate, null);
第三个参数是要查询的日历,要使用所有日历,请传递null。
创建NSPredicate之后,在EventStore上使用EventsMatching方法,执行查询:
EKCalendarItem[] events = App.Current.EventStore.EventsMatching (query);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句