Cordova calendar plugin for Windows Phone 8

寵の児 提交于 2019-12-24 12:29:05

问题


I am looking for a cordova plugin to add events to Windows Phone 8 calendar. There is no plugin on cordova plugin registry. My workaround was to write native plugin-

public void addCalendarEvents(String str)
        {
            string[] calendarValues = str.Split('|');           

            SaveAppointmentTask saveAppointmentTask = new SaveAppointmentTask();

            int appointmentYear = Int32.Parse(calendarValues[3]);
            int appointmentMonth = Int32.Parse(calendarValues[4]);
            int appointmentDate = Int32.Parse(calendarValues[5]);
            float appointmentTime = float.Parse(calendarValues[6]);

            DateTime scheduleApptDateStart = (new DateTime(appointmentYear, appointmentMonth, appointmentDate, 7, 0, 0)).AddHours(appointmentTime);
            DateTime scheduleApptDateEnd = (new DateTime(appointmentYear, appointmentMonth, appointmentDate, 7, 30, 0)).AddHours(appointmentTime);
            saveAppointmentTask.StartTime = scheduleApptDateStart;
            saveAppointmentTask.EndTime = scheduleApptDateEnd;
            saveAppointmentTask.Subject = calendarValues[1];
            saveAppointmentTask.Location = calendarValues[2];
            saveAppointmentTask.Details = "";
            saveAppointmentTask.IsAllDayEvent = false;
            saveAppointmentTask.Reminder = Reminder.FifteenMinutes;
            saveAppointmentTask.AppointmentStatus = Microsoft.Phone.UserData.AppointmentStatus.Busy;
            saveAppointmentTask.Show();
        }

and call it using

 var inputCalendarString = notes + '|' + title + '|' + location + '|' + appointmentDate.getFullYear() + '|' + (appointmentDate.getMonth() + 1) + '|' + appointmentDate.getDate() + '|' + '1.0' + '|' + ' ';
                cordova.exec(null, null, "AddCalendarEvents", "addCalendarEvents", inputCalendarString);

It works for one event but if I have loop of events its not working. Its not going in cordova success callback. If anybody wrote such plugin, it would be of really great help.


回答1:


Where have you stated the success callback ? According to me the code in your js should be -

cordova.exec(successCallback, failureCallback, 'AddCalendarEvents', 'addCalendarEvents', inputCalendarString);

 function successCallback(success){
     console.log('Success');
 }

 function failureCallback(error){
     console.log('Failure');
 }

Also, you'll need a DispatcherCommandResult in your .cs file to return the callback.




回答2:


I did a workaround. Maintain localstorage flag, data with current index of events data. use resume callback to add rest of the events using custom appointment plugin that you wrote. Each time your app resumes you increment index and add events data from next index.

document.addEventListener('resume', this.resumeApp, false)
 resumeApp: function () {
        if (localStorage.getItem('updatecalendar') == 'false') {
            syncUpdatedCalendarWP8();
        }
    },



回答3:


Use the following plugin that allows for adding of events to the native Windows Phone calendar https://github.com/faGH/fa-plugin-calendar




回答4:


Create a new cs file in plugins directory named AddCalendarEvents.cs and add following code-

using Microsoft.Phone.Tasks;
using Microsoft.Phone.UserData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands;
using WPCordovaClassLib.Cordova.JSON;

namespace Cordova.Extension.Commands {
    public class AddCalendarEvents: BaseCommand {
        public void addCalendarEvents(String str) {
            string[] calendarValues = str.Split('|');

            SaveAppointmentTask saveAppointmentTask = new SaveAppointmentTask();

            int appointmentYear = Int32.Parse(calendarValues[3]);
            int appointmentMonth = Int32.Parse(calendarValues[4]);
            int appointmentDate = Int32.Parse(calendarValues[5]);
            float appointmentTime = float.Parse(calendarValues[6]);

            DateTime scheduleApptDateStart = (new DateTime(appointmentYear, appointmentMonth, appointmentDate, 7, 0, 0)).AddHours(appointmentTime);
            DateTime scheduleApptDateEnd = (new DateTime(appointmentYear, appointmentMonth, appointmentDate, 7, 30, 0)).AddHours(appointmentTime);
            saveAppointmentTask.StartTime = scheduleApptDateStart;
            saveAppointmentTask.EndTime = scheduleApptDateEnd;
            saveAppointmentTask.Subject = calendarValues[1];
            saveAppointmentTask.Location = calendarValues[2];
            saveAppointmentTask.Details = "";
            saveAppointmentTask.IsAllDayEvent = false;
            saveAppointmentTask.Reminder = Reminder.FifteenMinutes;
            saveAppointmentTask.AppointmentStatus = Microsoft.Phone.UserData.AppointmentStatus.Busy;
            saveAppointmentTask.Show();
        }

        public void getCalendarEventData(String str) {
            ButtonAppointments_Click();
        }

        private void ButtonAppointments_Click() {
            Appointments appts = new Appointments();

            //Identify the method that runs after the asynchronous search completes.
            appts.SearchCompleted += new EventHandler < AppointmentsSearchEventArgs > (Appointments_SearchCompleted);

            DateTime start = DateTime.Now;
            DateTime end = start.AddDays(7);
            int max = 20;

            //Start the asynchronous search.
            appts.SearchAsync(start, end, max, "Appointments Test #1");
        }

        void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e) {
            //Do something with the results.
            //MessageBox.Show(e.Results.Count().ToString());
            try {
                e.Results.ToList();
                MessageBox.Show("Success");
            } catch (System.Exception) {}

        }
    }
}

Refer plugin in config.xml-

<feature name="AddCalendarEvents">
        <param name="wp-package" value="AddCalendarEvents" />
        <param name="onload" value="true" />
    </feature>

You can call it using

var inputCalendarString = notes + '|' + title + '|' + location + '|' + appointmentDate.getFullYear() + '|' + (appointmentDate.getMonth() + 1) + '|' + appointmentDate.getDate() + '|' + '1.0' + '|' + ' ';
cordova.exec(null, null, "AddCalendarEvents", "addCalendarEvents", inputCalendarString);

It works for one event but if you have loop of events it will not work. To support multiple events, you can maintain Localstorage flag & data with current index of events data. Use resume callback to add rest of the events using custom appointment plugin that you wrote. Each time your app resumes you increment index and add events data from next index.

document.addEventListener('resume', this.resumeApp, false)
resumeApp: function () {
if (localStorage.getItem('updatecalendar') == 'false') {
syncUpdatedCalendarWP8();
      }
}

Reference



来源:https://stackoverflow.com/questions/28166510/cordova-calendar-plugin-for-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!