Windows Phone 8的Cordova日历插件 [英] Cordova calendar plugin for Windows Phone 8

查看:152
本文介绍了Windows Phone 8的Cordova日历插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个cordova插件来添加事件到Windows Phone 8日历。 cordova插件注册表中没有插件。我的解决方法是编写原生插件 -

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();
        }

并使用

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

它适用于一个事件,但如果我有事件循环它不工作。它不会在cordova成功回调。

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.

推荐答案

在plugins目录中创建一个名为AddCalendarEvents的新cs文件。 cs并添加以下代码 -

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) {}

        }
    }
}

在config.xml中引用插件 -

Refer plugin in config.xml-

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

您可以使用

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

它适用于一个事件,但如果你有循环的事件,它不会工作。要支持多个事件,可以维护Localstorage标志&数据与事件数据的当前索引。使用resume callback以使用您编写的自定义约会插件添加其余事件。每次您的应用程序恢复时,您都会增加索引并从下一个索引中添加事件数据。

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();
      }
}

参考资料

这篇关于Windows Phone 8的Cordova日历插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆