private void button1_Click(object sender, RoutedEventArgs e)
        {
            var contactChooser = new ContactChooserTask();

            contactChooser.OnSelected += new EventHandler <ContactChooserEventArgs>(contactChooser_OnSelected);
            contactChooser.Show();
        }
 private void button1_Click(object sender, RoutedEventArgs e)
 {
     var contactChooser = new ContactChooserTask();
     contactChooser.OnSelected += new EventHandler<ContactChooserEventArgs>(contactChooser_OnSelected);
     contactChooser.Show();
 }
        /// <summary>
        /// Invoke the method specified by the
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </summary>
        /// <param name="message">
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </param>
        internal async override void InvokeAsync(JSMessage message)
        {
            string        callbackId = message.CallbackId;
            List <string> properties = null;

            try
            {
                switch (message.Method)
                {
                case "callContact":

                    Logger.Info("Calling contact...");

                    // Get the phone number
                    string phonenumber = message.Args[0] as string;

                    // Create and show the PhoneCall task
                    PhoneCallTask phoneCallTask = new PhoneCallTask();
                    phoneCallTask.PhoneNumber = phonenumber;
                    UiDispatcher.BeginInvoke(() =>
                    {
                        phoneCallTask.Show();
                    });

                    // Set app navigated to external page
                    Mowbly.AppNavigatedToExternalPage = true;
                    break;

                case "deleteContact":

                    Logger.Info("Deleting contact...");

                    string contactId = message.Args[0] as string;

                    try
                    {
                        ContactStore contactStore = await ContactStore.CreateOrOpenAsync(
                            ContactStoreSystemAccessMode.ReadWrite,
                            ContactStoreApplicationAccessMode.ReadOnly);

                        await contactStore.DeleteContactAsync(contactId);

                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = true
                        });
                    }
                    catch (Exception e)
                    {
                        string error = String.Concat(
                            Mowbly.GetString(Constants.STRING_CONTACT_DELETE_ERROR),
                            e.Message);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code  = MethodResult.FAILURE_CODE,
                            Error = new MethodError
                            {
                                Message = error
                            }
                        });
                    }
                    break;

                case "findContact":

                    // Read args
                    string filter  = message.Args[0] as string;
                    JToken options = message.Args[1] as JToken;
                    properties = options["properties"].ToObject <List <string> >();
                    double limit = (double)options["limit"];

                    Logger.Info("Searching contacts for " + filter);

                    // Contacts search results handler
                    EventHandler <MowblyContactsSearchEventArgs> OnContactsSearchCompleted = null;
                    OnContactsSearchCompleted = (sender, e) =>
                    {
                        if (e.Status)
                        {
                            // Notify result to JS
                            InvokeCallbackJavascript(message.CallbackId, new MethodResult {
                                Result = e.W3Contacts
                            });
                        }
                        else
                        {
                            InvokeCallbackJavascript(message.CallbackId, new MethodResult {
                                Result = null
                            });
                        }
                    };

                    if (Regex.IsMatch(filter, @"^[0-9()-]+$"))
                    {
                        // Only numbers, search by phone number
                        SearchContactInUserDataAsync(filter, OnContactsSearchCompleted, true, FilterKind.PhoneNumber, limit, properties);
                    }
                    else
                    {
                        // Search by display name
                        SearchContactInUserDataAsync(filter, OnContactsSearchCompleted, true, FilterKind.DisplayName, limit, properties);
                    }
                    break;

                case "pickContact":


                    properties = ((JToken)message.Args[0]).ToObject <List <string> >();
                    ContactChooserTask contactChooserTask = new ContactChooserTask(callbackId);
                    EventHandler <ContactChooserTaskEventArgs> OnContactChooserTaskCompleted = null;
                    OnContactChooserTaskCompleted = (sender, e) =>
                    {
                        // Unsubscribe
                        contactChooserTask.OnCompleted -= OnContactChooserTaskCompleted;

                        // Notify result to JS
                        if (e.Contact != null)
                        {
                            W3Contact contact = new W3Contact(e.Contact, properties);
                            InvokeCallbackJavascript(e.CallbackId, new MethodResult {
                                Result = contact
                            });
                        }
                        else
                        {
                            InvokeCallbackJavascript(e.CallbackId, new MethodResult
                            {
                                Code  = MethodResult.FAILURE_CODE,
                                Error = new MethodError
                                {
                                    Message = Mowbly.GetString(Constants.STRING_ACTIVITY_CANCELLED)
                                }
                            });
                        }
                    };

                    // Subscribe to OnCompleted event
                    contactChooserTask.OnCompleted += OnContactChooserTaskCompleted;

                    // Show contact chooser task
                    UiDispatcher.BeginInvoke(() =>
                    {
                        try
                        {
                            contactChooserTask.Show();
                        }
                        catch (Exception e)
                        {
                            // Might fail at times since navigation is not allowed when task is not in foreground
                            string error = String.Concat(Mowbly.GetString(Constants.STRING_CONTACT_CHOOSE_FAILED), e.Message);
                            Logger.Error(error);
                            InvokeCallbackJavascript(callbackId, new MethodResult
                            {
                                Code  = MethodResult.FAILURE_CODE,
                                Error = new MethodError
                                {
                                    Message = error
                                }
                            });
                        }
                    });
                    break;

                case "saveContact":

                    try
                    {
                        // Create W3Contact object from JS contact
                        W3Contact w3Contact       = JsonConvert.DeserializeObject <W3Contact>(message.Args[0].ToString());
                        string    storedContactId = await w3Contact.SaveToNativeContactStore();

                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Result = storedContactId
                        });
                    }
                    catch (Exception e)
                    {
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_CONTACT_SAVE_FAILED), e.Message);
                        Logger.Error(error);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code  = MethodResult.FAILURE_CODE,
                            Error = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                case "viewContact":

                    // Get the param and type
                    string id = message.Args[0] as string;
                    Tuple <string, string> paramAndType = await GetSearchParamAndType(id);

                    if (paramAndType != null)
                    {
                        string            param             = paramAndType.Item1;
                        string            type              = paramAndType.Item2;
                        ContactViewerTask contactViewerTask = new ContactViewerTask(param, type);

                        // Show contact viewer task
                        UiDispatcher.BeginInvoke(() =>
                        {
                            try
                            {
                                contactViewerTask.Show();
                            }
                            catch (Exception e)
                            {
                                // Might fail at times since navigation is not allowed when task is not in foreground
                                string error = String.Concat(Mowbly.GetString(Constants.STRING_CONTACT_VIEW_FAILED), e.Message);
                                Logger.Error(error);
                                InvokeCallbackJavascript(callbackId, new MethodResult
                                {
                                    Code  = MethodResult.FAILURE_CODE,
                                    Error = new MethodError
                                    {
                                        Message = error
                                    }
                                });
                            }
                        });
                    }
                    else
                    {
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_CONTACT_VIEW_FAILED),
                                                     Mowbly.GetString(Constants.STRING_CONTACT_NOT_FOUND));
                        Logger.Error(error);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code  = MethodResult.FAILURE_CODE,
                            Error = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                default:
                    Logger.Error("Feature " + Name + " does not support method " + message.Method);
                    break;
                }
            }
            catch (Exception e)
            {
                Logger.Error("Exception occured. Reason - " + e.Message);
            }
        }