Beispiel #1
0
 private async  void bt_add_Click(object sender, RoutedEventArgs e)
 {
     //创建一个系统通信可以读写和其他程序制度的联系人存储
     ContactStore conStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     //新增联系人
     ContactInformation conInfo = new ContactInformation();
     //获取ContacInformation类的属性map表
     var properties = await conInfo.GetPropertiesAsync();
     //添加电话属性
     properties.Add(KnownContactProperties.FamilyName, "test");
     properties.Add(KnownContactProperties.Telephone, "123456789");
  
     //创建联系人对象
     StoredContact storedContact = new StoredContact(conStore, conInfo);
     //获取安装包的一张图片文件用作联系人的头像
     StorageFile imagefile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets/1.jpeg");
         
     
     ///设置头像,将图片数据转化成Stream对象,在转化成IInputStream对象。
     //打开文件的可读数据流
     Stream stream = await imagefile.OpenStreamForReadAsync();
     //用Stream对象转化成为IIputStream对象
     IInputStream inputStream = stream.AsInputStream();
     //用IInputStream 对象设置为联系人头像
     await storedContact.SetDisplayPictureAsync(inputStream);
     //保存联系人
     await storedContact.SaveAsync();
     
     ///获取头像,接收到的图片数据为IRandomAccessStream类型,如果要展示出来,需要创建一个BitmapImage对象,
     IRandomAccessStream raStream = await storedContact.GetDisplayPictureAsync();
     BitmapImage bi = new BitmapImage();
     bi.SetSource(raStream);
     image.Source = bi;
 }
 public async Task SetPictureOnStoredContactAsync(StoredContact contact)
 {
     try
     {
         await contact.SetDisplayPictureAsync(FileManager.ReadAsStream(
                                                  FileManager.GetAbsolutePath(PhotoPath)));
     }
     catch (Exception e)
     {
         Logger.Error("Error setting photo on stored contact. Reason - " + e.Message);
     }
 }
 private async void Button_Click_1(object sender, RoutedEventArgs e)
 {
     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     ContactInformation contactInformation=new ContactInformation();
     var properties = await contactInformation.GetPropertiesAsync();
     properties.Add(KnownContactProperties.FamilyName, name.Text);
     properties.Add(KnownContactProperties.Telephone, tel.Text);
     
     StoredContact storedContact = new StoredContact(contactStore, contactInformation);
     if (stream != null)
     {
         await storedContact.SetDisplayPictureAsync(stream);
     }
     await storedContact.SaveAsync();
     NavigationService.GoBack();
 }
Beispiel #4
0
        private async void bt_add_Click(object sender, RoutedEventArgs e)
        {
            //创建一个系统通信可以读写和其他程序制度的联系人存储
            ContactStore conStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);

            //新增联系人
            ContactInformation conInfo = new ContactInformation();
            //获取ContacInformation类的属性map表
            var properties = await conInfo.GetPropertiesAsync();

            //添加电话属性
            properties.Add(KnownContactProperties.FamilyName, "test");
            properties.Add(KnownContactProperties.Telephone, "123456789");

            //创建联系人对象
            StoredContact storedContact = new StoredContact(conStore, conInfo);
            //获取安装包的一张图片文件用作联系人的头像
            StorageFile imagefile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets/1.jpeg");


            ///设置头像,将图片数据转化成Stream对象,在转化成IInputStream对象。
            //打开文件的可读数据流
            Stream stream = await imagefile.OpenStreamForReadAsync();

            //用Stream对象转化成为IIputStream对象
            IInputStream inputStream = stream.AsInputStream();
            //用IInputStream 对象设置为联系人头像
            await storedContact.SetDisplayPictureAsync(inputStream);

            //保存联系人
            await storedContact.SaveAsync();

            ///获取头像,接收到的图片数据为IRandomAccessStream类型,如果要展示出来,需要创建一个BitmapImage对象,
            IRandomAccessStream raStream = await storedContact.GetDisplayPictureAsync();

            BitmapImage bi = new BitmapImage();

            bi.SetSource(raStream);
            image.Source = bi;
        }
Beispiel #5
0
        private async Task SetContactProperties(StoredContact contact, User user, User originalUser = null)
        {
            if (contact != null)
            {
                contact.RemoteId   = ContactsManager.GetRemoteId(user);
                contact.GivenName  = user.first_name;
                contact.FamilyName = user.last_name;
                if (!string.IsNullOrWhiteSpace(user.photo_max) && !user.photo_max.EndsWith(".gif"))
                {
                    Stream responseStreamAsync = await HttpExtensions.TryGetResponseStreamAsync(user.photo_max);

                    if (responseStreamAsync == null)
                    {
                        throw new Exception("failed to download contact pic " + user.photo_max);
                    }
                    await contact.SetDisplayPictureAsync(responseStreamAsync.AsInputStream());
                }
                IDictionary <string, object> propertiesAsync = await contact.GetPropertiesAsync();

                if (!string.IsNullOrWhiteSpace(user.site))
                {
                    propertiesAsync[KnownContactProperties.Url] = (object)user.site;
                }
                if (!string.IsNullOrWhiteSpace(user.mobile_phone) && this.IsPhoneNumber(user.mobile_phone))
                {
                    List <string> phoneNumbers = BaseFormatterHelper.ParsePhoneNumbers(user.mobile_phone);
                    if (phoneNumbers.Count >= 1)
                    {
                        propertiesAsync[KnownContactProperties.MobileTelephone] = (object)phoneNumbers[0];
                    }
                    if (phoneNumbers.Count >= 2)
                    {
                        propertiesAsync[KnownContactProperties.AlternateMobileTelephone] = (object)phoneNumbers[1];
                    }
                }
                if (!string.IsNullOrWhiteSpace(user.home_phone) && this.IsPhoneNumber(user.home_phone))
                {
                    List <string> phoneNumbers = BaseFormatterHelper.ParsePhoneNumbers(user.home_phone);
                    if (phoneNumbers.Count >= 1)
                    {
                        propertiesAsync[KnownContactProperties.Telephone] = (object)phoneNumbers[0];
                    }
                    if (phoneNumbers.Count >= 2)
                    {
                        propertiesAsync[KnownContactProperties.AlternateTelephone] = (object)phoneNumbers[1];
                    }
                }
                DateTime dateTime;
                if (!string.IsNullOrWhiteSpace(user.bdate) && ContactsManager.TryParseDateTimeFromString(user.bdate, out dateTime))
                {
                    TimeSpan timeSpan = DateTime.Now - DateTime.UtcNow;
                    dateTime = dateTime.Add(timeSpan);
                    DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, TimeSpan.Zero);
                    propertiesAsync[KnownContactProperties.Birthdate] = (object)new DateTimeOffset(dateTime);
                }
            }
            if (originalUser == null)
            {
                return;
            }
            originalUser.first_name   = user.first_name;
            originalUser.last_name    = user.last_name;
            originalUser.site         = user.site;
            originalUser.mobile_phone = user.mobile_phone;
            originalUser.home_phone   = user.home_phone;
            originalUser.photo_max    = user.photo_max;
            originalUser.bdate        = user.bdate;
        }
Beispiel #6
0
        public async Task <StoredContact> ToStoredContact()
        {
            StoredContact contact = new StoredContact(App.ContactStore);

            contact.RemoteId = this.GenerateLocalId();
            contact.RemoteId = await App.RemoteIdHelper.GetTaggedRemoteId(App.ContactStore, contact.RemoteId);

            if (String.IsNullOrEmpty(this.GivenName) == false)
            {
                contact.GivenName = this.GivenName;
            }

            if (String.IsNullOrEmpty(this.FamilyName) == false)
            {
                contact.FamilyName = this.FamilyName;
            }


            #region Set Picture ; Test = not yet
            if (this.Thumbnail != null)
            {
                using (IInputStream inStream = await this.Thumbnail.OpenSequentialReadAsync())
                {
                    await contact.SetDisplayPictureAsync(inStream);
                }
            }
            #endregion


            var props = await contact.GetPropertiesAsync();

            if (String.IsNullOrEmpty(this.Nickname) == false)
            {
                props.Add(KnownContactProperties.Nickname, this.Nickname);
            }
            if (String.IsNullOrEmpty(this.Mobilephone) == false)
            {
                props.Add(KnownContactProperties.MobileTelephone, this.Mobilephone);
            }
            if (String.IsNullOrEmpty(this.AlternateMobilePhone) == false)
            {
                props.Add(KnownContactProperties.AlternateMobileTelephone, this.AlternateMobilePhone);
            }
            if (String.IsNullOrEmpty(this.Telephone) == false)
            {
                props.Add(KnownContactProperties.Telephone, this.Telephone);
            }
            if (string.IsNullOrEmpty(this.Email) == false)
            {
                props.Add(KnownContactProperties.Email, this.Email);
            }
            if (String.IsNullOrEmpty(this.Address) == false)
            {
                props.Add(KnownContactProperties.Address, new ContactAddress()
                {
                    Locality      = this.Address,
                    StreetAddress = (this.Street == null) ? "":this.Street,
                    Region        = (this.Region == null) ? "" : this.Region,
                    Country       = (this.Country == null) ? "" : this.Country
                }
                          );
            }

            if (this.Birthday != null)
            {
                props.Add(
                    KnownContactProperties.Birthdate,
                    new DateTimeOffset(
                        new DateTime(this.Birthday.Year, this.Birthday.Month, this.Birthday.Day),
                        new TimeSpan(1, 0, 0)));
            }

            return(contact);
        }
        public async Task AddContact(RemoteContact remoteContact)
        {
            try 
            { 
                 ContactStore store = await ContactStore.CreateOrOpenAsync();

                 StoredContact contact = new StoredContact(store);
                 
                 RemoteIdHelper remoteIDHelper = new RemoteIdHelper();
                 contact.RemoteId = await remoteIDHelper.GetTaggedRemoteId(store, remoteContact.RemoteId);

                 contact.GivenName = remoteContact.GivenName;
                 contact.FamilyName = remoteContact.FamilyName;

                 IDictionary<string, object> props = await contact.GetPropertiesAsync();
                 props.Add(KnownContactProperties.Email, remoteContact.Email);

                 IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync();
                 extprops.Add("Codename", remoteContact.CodeName);

               
                 if (remoteContact.DisplayName != null) contact.DisplayName = remoteContact.DisplayName;
                 IInputStream I = remoteContact.photo.AsInputStream();
                 await contact.SetDisplayPictureAsync(I);

                 Debug.WriteLine(contact.Store);

                 await contact.SaveAsync();
                 Debug.WriteLine(String.Format("Adding:\n{0}", remoteContact.ToString()));
            }

            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Beispiel #8
0
        private async Task SetContactProperties(StoredContact contact, User user, User originalUser = null)
        {
            if (contact != null)
            {
                contact.RemoteId   = (ContactsManager.GetRemoteId(user));
                contact.GivenName  = (user.first_name);
                contact.FamilyName = (user.last_name);
                if (!string.IsNullOrWhiteSpace(user.photo_max) && !user.photo_max.EndsWith(".gif"))
                {
                    Stream stream = await HttpExtensions.TryGetResponseStreamAsync(user.photo_max);

                    if (stream == null)
                    {
                        throw new Exception("failed to download contact pic " + user.photo_max);
                    }
                    await contact.SetDisplayPictureAsync(stream.AsInputStream());
                }
                IDictionary <string, object> dictionary = await contact.GetPropertiesAsync();

                if (!string.IsNullOrWhiteSpace(user.site))
                {
                    dictionary[KnownContactProperties.Url] = user.site;
                }
                if (!string.IsNullOrWhiteSpace(user.mobile_phone) && this.IsPhoneNumber(user.mobile_phone))
                {
                    List <string> var_6_262 = BaseFormatterHelper.ParsePhoneNumbers(user.mobile_phone);
                    if (var_6_262.Count >= 1)
                    {
                        dictionary[KnownContactProperties.MobileTelephone] = var_6_262[0];
                    }
                    if (var_6_262.Count >= 2)
                    {
                        dictionary[KnownContactProperties.AlternateMobileTelephone] = var_6_262[1];
                    }
                }
                if (!string.IsNullOrWhiteSpace(user.home_phone) && this.IsPhoneNumber(user.home_phone))
                {
                    List <string> var_7_2D8 = BaseFormatterHelper.ParsePhoneNumbers(user.home_phone);
                    if (var_7_2D8.Count >= 1)
                    {
                        dictionary[KnownContactProperties.Telephone] = var_7_2D8[0];
                    }
                    if (var_7_2D8.Count >= 2)
                    {
                        dictionary[KnownContactProperties.AlternateTelephone] = var_7_2D8[1];
                    }
                }
                DateTime var_8;
                if (!string.IsNullOrWhiteSpace(user.bdate) && ContactsManager.TryParseDateTimeFromString(user.bdate, out var_8))
                {
                    var_8 = var_8.Add(DateTime.Now - DateTime.UtcNow);
                    new DateTimeOffset(var_8.Year, var_8.Month, var_8.Day, 0, 0, 0, 0, TimeSpan.Zero);
                    dictionary[KnownContactProperties.Birthdate] = new DateTimeOffset(var_8);
                }
            }
            if (originalUser != null)
            {
                originalUser.first_name   = user.first_name;
                originalUser.last_name    = user.last_name;
                originalUser.site         = user.site;
                originalUser.mobile_phone = user.mobile_phone;
                originalUser.home_phone   = user.home_phone;
                originalUser.photo_max    = user.photo_max;
                originalUser.bdate        = user.bdate;
            }
        }
Beispiel #9
0
        public async void AddContact(string remoteId, string givenName, string familyName, string username, String avatar, String url)
        {
            ContactStore store = await ContactStore.CreateOrOpenAsync();

            try
            {
                if (await store.FindContactByRemoteIdAsync(remoteId) == null)
                {
                    StoredContact contact = new StoredContact(store);

                    contact.RemoteId = remoteId;


                    contact.GivenName  = givenName;
                    contact.FamilyName = familyName;

                    contact.DisplayName = remoteId;


                    HttpWebRequest  request     = (HttpWebRequest)WebRequest.Create(new Uri(avatar));
                    HttpWebResponse webResponse = await request.GetResponseAsync() as HttpWebResponse;

                    MemoryStream memoryStream = new MemoryStream();
                    webResponse.GetResponseStream().CopyTo(memoryStream);
                    IRandomAccessStream stream = await ConvertToRandomAccessStream(memoryStream);

                    IDictionary <string, object> props = await contact.GetPropertiesAsync();

                    props.Add(KnownContactProperties.Nickname, username);
                    props.Add(KnownContactProperties.Url, url);



                    IDictionary <string, object> extprops = await contact.GetExtendedPropertiesAsync();

                    extprops.Add("Codename", username);
                    extprops.Add("ProfilePic", avatar);


                    await contact.SetDisplayPictureAsync(stream);

                    await contact.SaveAsync();
                }
                else
                {
                    StoredContact contact = await store.FindContactByRemoteIdAsync(remoteId);

                    IDictionary <string, object> extprops = await contact.GetExtendedPropertiesAsync();

                    if (!extprops.Values.Contains(avatar))
                    {
                        HttpWebRequest  request     = (HttpWebRequest)WebRequest.Create(new Uri(avatar));
                        HttpWebResponse webResponse = await request.GetResponseAsync() as HttpWebResponse;

                        MemoryStream memoryStream = new MemoryStream();
                        webResponse.GetResponseStream().CopyTo(memoryStream);
                        IRandomAccessStream stream = await ConvertToRandomAccessStream(memoryStream);

                        await contact.SetDisplayPictureAsync(stream);

                        extprops.Remove("ProfilePic");
                        extprops.Add("ProfilePic", avatar);

                        await contact.SaveAsync();
                    }
                }
            }
            catch (WebException) { }
        }