Опубликован: 01.11.2011 | Доступ: свободный | Студентов: 1424 / 63 | Оценка: 3.84 / 3.44 | Длительность: 15:38:00
Специальности: Программист
Практическая работа 24:

Контакты и календарь в Windows Phone 7

Далее, откройте файл кода MainPage.xaml.cs и введите следующий код:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using Microsoft.Phone.UserData;

namespace ContactsAndCalendarTestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        FilterKind contactFilterKind = FilterKind.None;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            name.IsChecked = true;

            ContactAccounts.DataContext = (new Contacts()).Accounts;
            CalendarAccounts.DataContext = (new Appointments()).Accounts;
        }


        //-------------------------------------------------------------------------------
        //-- Contact Methods
        //-------------------------------------------------------------------------------
        private void SearchContacts_Click(object sender, RoutedEventArgs e)
        {
            ContactResultsLabel.Text = "Загружаю результаты ...";
            ContactResultsData.DataContext = null;

            Contacts cons = new Contacts();

            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

            cons.SearchAsync(contactFilterString.Text, contactFilterKind, "Contacts Test #1");
        }


        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            //MessageBox.Show(e.State.ToString());

            try
            {
                //Bind the results to the listbox that displays them in the UI
                ContactResultsData.DataContext = e.Results;
            }
            catch (System.Exception)
            {
                //That's okay, no results
            }

            if (ContactResultsData.Items.Count > 0)
            {
                ContactResultsLabel.Text = "Результаты (Раскройте подробности ...)";
            }
            else
            {
                ContactResultsLabel.Text = "Ничего не найдено";
            }
        }


        private void FilterChange(object sender, RoutedEventArgs e)
        {
            String option = ((RadioButton)sender).Name;

            InputScope scope = new InputScope();
            InputScopeName scopeName = new InputScopeName();

            switch (option)
            {
                case "name":
                    contactFilterKind = FilterKind.DisplayName;
                    scopeName.NameValue = InputScopeNameValue.Text;
                    break;

                case "phone":
                    contactFilterKind = FilterKind.PhoneNumber;
                    scopeName.NameValue = InputScopeNameValue.TelephoneNumber;
                    break;

                case "email":
                    contactFilterKind = FilterKind.EmailAddress;
                    scopeName.NameValue = InputScopeNameValue.EmailSmtpAddress;
                    break;

                default:
                    contactFilterKind = FilterKind.None;
                    break;
            }

            scope.Names.Add(scopeName);
            contactFilterString.InputScope = scope;
            contactFilterString.Focus();
        }


        private void ContactResultsData_Tap(object sender, GestureEventArgs e)
        {
            App.con = ((sender as ListBox).SelectedValue as Contact);

            NavigationService.Navigate(new Uri("/ContactDetails.xaml", UriKind.Relative));
        }


        //-------------------------------------------------------------------------------
        //-- Appointment Methods
        //-------------------------------------------------------------------------------
        private void SearchAppointments_Click(object sender, RoutedEventArgs e)
        {
            AppointmentResultsLabel.Text = "Загружаю результаты ...";
            AppointmentResultsData.DataContext = null;
            Appointments appts = new Appointments();

            appts.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(Appointments_SearchCompleted);

            DateTime start = new DateTime();
            start = DateTime.Now;
            //MessageBox.Show(start.ToLongDateString());

            DateTime end = new DateTime();
            end = start.AddDays(7);
            //MessageBox.Show(end.ToLongDateString());

            appts.SearchAsync(start, end, 20, "Appointments Test #1");
        }


        void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
        {
            //MessageBox.Show(e.State.ToString());

            StartDate.Text = e.StartTimeInclusive.ToShortDateString();
            EndDate.Text = e.EndTimeInclusive.ToShortDateString();

            try
            {
                //Bind the results to the listbox that displays them in the UI
                AppointmentResultsData.DataContext = e.Results;
            }
            catch (System.Exception)
            {
                //That's okay, no results
            }

            if (AppointmentResultsData.Items.Count > 0)
            {
                AppointmentResultsLabel.Text = "Результаты (Нажмите для получения деталей...)";
            }
            else
            {
                AppointmentResultsLabel.Text = "Ничего не найдено";
            }
        }


        private void AppointmentResultsData_Tap(object sender, GestureEventArgs e)
        {
            App.appt = ((sender as ListBox).SelectedValue as Appointment);

            NavigationService.Navigate(new Uri("/AppointmentDetails.xaml", UriKind.Relative));
        }
    }//End page class


    //-------------------------------------------------------------------------------
    //-- Contact Photo Converter
    //-------------------------------------------------------------------------------
    public class ContactPictureConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Contact c = value as Contact;
            if (c == null) return null;

            System.IO.Stream imageStream = c.GetPicture();
            if (null != imageStream)
            {
                return Microsoft.Phone.PictureDecoder.DecodeJpeg(imageStream);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }//End converter class


}//End namespace