Xamarin.Forms 平台特定代码

Xamarin.Forms platform specific code

读者好, 我正在尝试使用一种用于 android 的方法 我如何将 ContentPage 转换为 PCL 中 android 的 Activity 甚至可能吗?

尝试使用指令,但它也不起作用

 public static void AddEvent(GSDEvents ev, Activity ac)
        {
            // Id++;
            Calendar cal = Calendar.GetInstance(Java.Util.TimeZone.Default, Locale.Default);
            ContentValues eventValues = new ContentValues();
            eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId, Id);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Title, ev.Title);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Description, ev.Description);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(ev.Year, ev.Month, ev.Day, ev.Hour, ev.Minute));
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(ev.Year, ev.Month, ev.Day, ev.Hour, ev.Minute));
            eventValues.Put(CalendarContract.Events.InterfaceConsts.AllDay, ev.AllDay ? "1" : "0");
            eventValues.Put(CalendarContract.Events.InterfaceConsts.HasAlarm, ev.HasAlarm ? "1" : "0");
            eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "GMT+1:00");
            eventValues.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "GMT+1:00");

            var uriCalendar = ac.ContentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);

            ContentValues remindervalues = new ContentValues();
            remindervalues.Put(CalendarContract.Reminders.InterfaceConsts.Minutes, ev.ReminderMinute);
            remindervalues.Put(CalendarContract.Reminders.InterfaceConsts.EventId, Id);
            remindervalues.Put(CalendarContract.Reminders.InterfaceConsts.Method, (int)Android.Provider.RemindersMethod.Alert);

            var uriCalendarReminder = ac.ContentResolver.Insert(CalendarContract.Reminders.ContentUri, remindervalues);

        }

在我的内容页面中

#if __ANDROID__

            GSDEvents ev = new GSDEvents();
            ev.Title = "Test Event";
            ev.Description = "Dit is nog een test van de beschrijving";
            GSDEvents.AddEvent(ev, __THEPROBLEM HOW CAN I GET THE ACTIVITY);    
#endif

有人知道我在 PCL 工作时如何做到这一点吗 希望有人能给我指出正确的方向。

亲切的问候, 斯特凡

你只有一个activity。主要是在您的 Droid 应用程序中找到它。如果您需要在 activity 中专门编写某些内容,请在此处对其进行编码,然后使用事件从您的 Xamarin 表单页面中启动它。页面与活动并没有真正相关,至少不是以您的思维方式。

Xamarin Forms 在设计上是跨平台的,因此如果您不能在 IOS、WP 和 Droid 上做到这一点,您也不能在 Forms 中做到这一点。由于显然在其他 2 个平台上没有活动,所以 Forms 在 PCL 中没有 activity 的概念。

我对 GSDEvent 的用途还不够熟悉,无法进一步提供帮助。但是要执行您想要的操作,您必须在您的 ContentPage 上放置一个事件。将代码分配给您的 Droid 项目中 Activity 的事件,并在您希望它运行时在您的 PCL 中触发它。

或者,您可以在 PCL 中创建一个带有事件的 Class 在您的 droid 项目中实例化它并分配事件,然后将其放入您可以从PCL.

我用 DependencyService Thnx 伙计们做到了:) https://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/