Developing for Windows Phones 7 and 8: Listing 1

Substitute Windows Phone 7 SaveAppointmentTask.

public class SaveAppointmentTask
{
    private static ConstructorInfo SaveAppointmentTaskConstructor { get; set; }
    private static Type ReminderType { get; set; }
    private static Type AppointmentStatusType { get; set; }
    private static MethodInfo ShowMethod { get; set; }

    static SaveAppointmentTask()
    {
        var type = Type.GetType("Microsoft.Phone.Tasks.SaveAppointmentTask, Microsoft.Phone");
        if (type == null) return;
        SaveAppointmentTaskConstructor = type.GetConstructor(new Type[] {});

        AppointmentStatusType = Type.GetType("Microsoft.Phone.UserData.AppointmentStatus, Microsoft.Phone");
        ReminderType = Type.GetType("Microsoft.Phone.Tasks.Reminder, Microsoft.Phone");
        ShowMethod = type.GetMethod("Show");
    }

    public static bool IsSupported 
    {
        get { return SaveAppointmentTaskConstructor != null; }
    }

    private object Instance { get; set; }

    public SaveAppointmentTask()
    {
        Instance = SaveAppointmentTaskConstructor.Invoke(null);
    }

    public DateTime? StartTime
    {
        get { return GetProperty<DateTime?>(Instance, "StartTime"); }
        set { SetProperty(Instance, "StartTime", value); }
    }

    public DateTime? EndTime
    {
        get { return GetProperty<DateTime?>(Instance, "StartTime"); }
        set { SetProperty(Instance, "StartTime", value); }
    }

    public bool IsAllDayEvent
    {
        get { return GetProperty<bool>(Instance, "IsAllDayEvent"); }
        set { SetProperty(Instance, "IsAllDayEvent", value); }
    }

    public string Subject
    {
        get { return GetProperty<string>(Instance, "Subject"); }
        set { SetProperty(Instance, "Subject", value); }
    }

    public string Location
    {
        get { return GetProperty<string>(Instance, "Location"); }
        set { SetProperty(Instance, "Location", value); }
    }

    public string Details
    {
        get { return GetProperty<string>(Instance, "Details"); }
        set { SetProperty(Instance, "Details", value); }
    }

    public Reminder Reminder
    {
        get { return GetEnumProperty<Reminder>(Instance, "Reminder"); }
        set { SetEnumProperty(Instance, "Reminder", value,ReminderType); }
    }
        
    public AppointmentStatus AppointmentStatus
    {
        get { return GetEnumProperty<AppointmentStatus>(Instance, "AppointmentStatus"); }
        set { SetEnumProperty(Instance, "AppointmentStatus", value,AppointmentStatusType); }
    }

    public void Show()
    {
        ShowMethod.Invoke(Instance, null);
    }

    private static void SetEnumProperty<TSource>(object instance, string name, TSource value,Type destinationType)
    {
        var actualValue = Enum.Parse(destinationType, value.ToString(), true);

        var setMethod = instance.GetType().GetProperty(name);
        setMethod.SetValue(instance, actualValue, null);
    }
    private static T GetEnumProperty<T>(object instance, string name)
    {
        var setMethod = instance.GetType().GetProperty(name);
        var enumValue= setMethod.GetValue(instance, null);
        var destValue = (T)Enum.Parse(typeof(T), enumValue.ToString(), true);
        return destValue;
    }

    private static void SetProperty<T>(object instance, string name, T value)
    {
        var setMethod = instance.GetType().GetProperty(name);
        setMethod.SetValue(instance, value, null);
    }
    private static T GetProperty<T>(object instance, string name)
    {
        var setMethod = instance.GetType().GetProperty(name);
        return (T)setMethod.GetValue(instance, null);
    }
}

public enum Reminder
{
    /// <summary>No reminder is shown for the appointment.</summary>
    None,
    /// <summary>The reminder is shown at the appointment start time.</summary>
    AtStartTime,
    /// <summary>The reminder is shown 5 minutes before the appointment.</summary>
    FiveMinutes,
    /// <summary>The reminder is shown 10 minutes before the appointment.</summary>
    TenMinutes,
    /// <summary>The reminder is shown 15 minutes before the appointment.</summary>
    FifteenMinutes,
    /// <summary>The reminder is shown 30 minutes before the appointment.</summary>
    ThirtyMinutes,
    /// <summary>The reminder is shown one hour before the appointment.</summary>
    OneHour,
    /// <summary>The reminder is shown 18 hours before the appointment.</summary>
    EighteenHours,
    /// <summary>The reminder is shown one day before the appointment.</summary>
    OneDay,
    /// <summary>The reminder is shown one 1 week before the appointment.</summary>
    OneWeek
}

/// <summary>Specifies information about how to treat the block of time of an appointment.</summary>
public enum AppointmentStatus
{
    /// <summary>The attendee is free during this appointment.</summary>
    Free,
    /// <summary>The attendee is tentatively busy during this appointment.</summary>
    Tentative,
    /// <summary>The attendee is busy during this appointment.</summary>
    Busy,
    /// <summary>The attendee is out of the office during this appointment.</summary>
    OutOfOffice
}

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events