TheAlmightyBob / Calendars

Cross-platform calendar API plugin for Xamarin and Windows
MIT License
101 stars 23 forks source link

How Can I Use #5

Closed mario-aleo closed 9 years ago

mario-aleo commented 9 years ago

Hello,

Sorry, but I don't have a clue about how to use the calendar plugin, if someone could help me, I'll appreciate. I would like to know what are the methods, how to call than and what is their callbacks, specially about how list the events.

Thanks.

TheAlmightyBob commented 9 years ago

I'm happy to help, and perhaps update the Readme as needed, but I'd like to understand where the problem is (that is, where I went wrong in communicating usage). Did you try accessing the static "CrossCalendars.Current" as mentioned in the Readme? That property is an ICalendars, which has comments for all its methods, so I was sort of counting on intellisense to take it from there in telling you what the methods were and how to use them. Curious if I made a false assumption here.

For your specific use, listing the events needs a calendar and a time range. CrossCalendars.Current.GetCalendarsAsync will return all the calendars on the device. From there you can select one and use CrossCalendars.Current.GetEventsAsync for the events.

mario-aleo commented 9 years ago

Sorry, now I've got but, but I'm stuck in a way to get calendar by calendar from CrossCalendars.Current.GetCalendarsAsync to use then in the CrossCalendars.Current.GetEventsAsync.

Thanks for the support.

TheAlmightyBob commented 9 years ago

Sorry, I don't quite understand what you're trying to do or what the problem is.

mario-aleo commented 9 years ago

Sorry, let me try to explain, CrossCalendars.Current.GetCalendarsAsync will return a list of calendars, once that some platforms like iOS keep more than one calendars in it, but the list is not an array and it seems not to be returning one by one calendar, it's returning the complete list. My question is, how can I get the calendar name from the list, one by one, if it`s not an array?

Thanks.

TheAlmightyBob commented 9 years ago

Er, it's not an array, but it is a list. Or is the Task part throwing you?

var calendars = await CrossCalendars.Current.GetCalendarsAsync(); foreach (var calendar in calendars) { ... }

mario-aleo commented 9 years ago

Maybe something like this so... although it isn't displaying anything

public List<string> listEvents = new List<string>();

        public MainContent ()
        {
            InitializeComponent ();

            getList ();

            var stackLayout = new StackLayout { Padding = 30, Spacing = 30 };
            var list = new ListView ();

            stackLayout.Children.Add (list);
            list.ItemsSource = listEvents;
        }

        public async void getList()
        {
            var listCalendars = await CrossCalendars.Current.GetCalendarsAsync();
            foreach(var calendar in listCalendars)
            {
                var events = await CrossCalendars.Current.GetEventsAsync(calendar, DateTime.Now, DateTime.Now.AddYears(1));

                foreach (var calendarEvent in events) 
                {
                    listEvents.Add(calendarEvent.ToString());
                }
            }
        }
TheAlmightyBob commented 9 years ago

I would expect that to work, assuming that your app has permission to access the calendar and there are actually events within the next year. Note that an issue like not having permission would throw an exception, which would be lost in your async void method (my other GitHub project, AsyncVoidAnalyzer, would be showing red squiggles).

mario-aleo commented 9 years ago

Got it working, but the list is only displayed if I change the state of the device (if I change the orientation).

Thanks for all your support.

TheAlmightyBob commented 9 years ago

Oh, that probably has to do with the fact that you're binding your Xamarin.Forms ListView to a List instead of ObservableCollection, so it doesn't know to update when you add the calendar entries to it.