From e9b05ed776b8fffdd6517e226e2805e07204fd06 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:31:41 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- .../src/wp/ContactPicker.xaml.cs | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 js/ui/cordova/plugins/cordova-plugin-contacts/src/wp/ContactPicker.xaml.cs diff --git a/js/ui/cordova/plugins/cordova-plugin-contacts/src/wp/ContactPicker.xaml.cs b/js/ui/cordova/plugins/cordova-plugin-contacts/src/wp/ContactPicker.xaml.cs new file mode 100644 index 0000000..ea3916e --- /dev/null +++ b/js/ui/cordova/plugins/cordova-plugin-contacts/src/wp/ContactPicker.xaml.cs @@ -0,0 +1,125 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +namespace WPCordovaClassLib.Cordova.Commands +{ + using System; + using System.Linq; + using System.Windows; + using System.Windows.Controls; + using System.Windows.Navigation; + using Microsoft.Phone.Tasks; + using Microsoft.Phone.UserData; + using DeviceContacts = Microsoft.Phone.UserData.Contacts; + + /// + /// Custom implemented class for picking single contact + /// + public partial class ContactPicker + { + #region Fields + + /// + /// Result of ContactPicker call, represent contact returned. + /// + private ContactPickerTask.PickResult result; + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + public ContactPicker() + { + InitializeComponent(); + var cons = new DeviceContacts(); + cons.SearchCompleted += this.OnSearchCompleted; + cons.SearchAsync(string.Empty, FilterKind.None, string.Empty); + } + + #endregion + + #region Callbacks + + /// + /// Occurs when contact is selected or pick operation cancelled. + /// + public event EventHandler Completed; + + #endregion + + /// + /// The on navigated from. + /// + /// + /// The e. + /// + protected override void OnNavigatedFrom(NavigationEventArgs e) + { + if (this.result == null) + { + this.Completed(this, new ContactPickerTask.PickResult(TaskResult.Cancel)); + } + + base.OnNavigatedFrom(e); + } + + /// + /// Called when contacts retrieval completed. + /// + /// The sender. + /// The instance containing the event data. + private void OnSearchCompleted(object sender, ContactsSearchEventArgs e) + { + if (e.Results.Count() != 0) + { + lstContacts.ItemsSource = e.Results.ToList(); + lstContacts.Visibility = Visibility.Visible; + NoContactsBlock.Visibility = Visibility.Collapsed; + } + else + { + lstContacts.Visibility = Visibility.Collapsed; + NoContactsBlock.Visibility = Visibility.Visible; + } + } + + /// + /// Called when any contact is selected. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void ContactsListSelectionChanged(object sender, SelectionChangedEventArgs e) + { + this.result = new ContactPickerTask.PickResult(TaskResult.OK) { Contact = e.AddedItems[0] as Contact }; + this.Completed(this, this.result); + + if (NavigationService.CanGoBack) + { + NavigationService.GoBack(); + } + } + } +} \ No newline at end of file