在 UWP 中使用 SaveContactAsync(contact) 时,我不断收到 "Value does not fall within an expected range"。我究竟做错了什么?

I keep getting "Value does not fall within an expected range" when using SaveContactAsync(contact) in UWP. What am I doing wrong?

我正在尝试从我的 UWP 应用程序连接和同步本地地址簿。

//Create or Find Contact List
                ContactList contactList;
                var contactLists = await store.FindContactListsAsync();
                if (0 == contactLists.Count) { contactList = await store.CreateContactListAsync("Practice Visuals"); }
                else { contactList = contactLists.Where(x => x.DisplayName == "Practice Visuals").FirstOrDefault(); }

                foreach (var p in Reporting.Patients.Take(10))
                {
                    Contact contact = null; try { contact = await contactList.GetContactAsync(p.PatNum); } catch { }
                    if (contact is null) //Contact not found hence create new contact
                    {
                        contact = new Contact()
                        {
                            Id = p.PatNum.ToString(),
                            RemoteId = p.PatNum.ToString(),
                            FirstName = p.FName,
                            LastName = p.LName,
                            MiddleName = p.MiddleI,
                            Nickname = p.Preferred,
                        };

                        contact.Emails.Add(new ContactEmail() { Address = p.Email, Kind = ContactEmailKind.Other });

                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Mobile, Number = p.WirelessPhone });
                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Home, Number = p.HmPhone });
                        contact.Phones.Add(new ContactPhone() { Kind = ContactPhoneKind.Work, Number = p.WkPhone });

                        //contact.Addresses.Add(new ContactAddress() { StreetAddress = p.Address + Environment.NewLine + p.Address2, Locality = p.City, Region = p.State, PostalCode = p.Zip });
                        //contact.ImportantDates.Add(new ContactDate() { Kind = ContactDateKind.Birthday, Day = (uint)((DateTime)p.Birthdate).Day, Month = (uint)((DateTime)p.Birthdate).Month, Year = ((DateTime)p.Birthdate).Year });
                        //contact.ImportantDates.Add(new ContactDate() { Kind = ContactDateKind.Anniversary, Day = (uint)((DateTime)p.DateFirstVisit).Day, Month = (uint)((DateTime)p.DateFirstVisit).Month, Year = ((DateTime)p.DateFirstVisit).Year });

                        //contact.Fields.Add(new ContactField("Clinic", new Converters.DataTypeValueConverter().Convert(p.ClinicNum, parameter: "ClinicNum"), ContactFieldType.Custom, ContactFieldCategory.None));
                        //contact.Fields.Add(new ContactField("Status", new Converters.DataTypeValueConverter().Convert(p.PatStatus, parameter: "PatStatus"), ContactFieldType.Custom, ContactFieldCategory.None));
                        //contact.Fields.Add(new ContactField("Gender", new Converters.DataTypeValueConverter().Convert(p.Gender, parameter: "Gender"), ContactFieldType.Custom, ContactFieldCategory.None));

                        await contactList.SaveContactAsync(contact);
                    }
                    else //Contact Found - check to see if update necessary
                    {

                    }
                }

                ContactsList.ItemsSource = await store.FindContactsAsync();

我已禁用 (//) 一些行以排除它们是问题所在,但问题仍然存在。

我该如何解决这个问题?

documentation 非常清楚该异常:

Throws a System.ArgumentException: 'Value does not fall within the expected range.' when the contact passed as parameter has a RemoteID set which is identical to a contacts RemoteID already saved on this device.

简而言之,请检查您正在通过的RemoteID

我试过你的代码来完成这个过程。我发现有一个小错误导致了这种行为。请不要在创建新的 ContactList 对象时定义 ContactList.Id Property

删除后,代码将正常工作。像下面这样:

 Contact contact = new Contact();

        contact.FirstName = "Jane";
        contact.LastName = "ContactPanelSample";
        contact.RemoteId = "3334";
        //contact.Id = "123";
        contact.Emails.Add(new ContactEmail { Address = "janedoe@example.com" });
        contact.Phones.Add(new ContactPhone { Number = "4255550123" });
        contact.SourceDisplayPicture = RandomAccessStreamReference.CreateFromUri(new Uri("https://docs.microsoft.com/en-us/windows/uwp/contacts-and-calendar/images/shoulder-tap-static-payload.png"));

        await contactList.SaveContactAsync(contact);

此外,还有一个官方示例展示了如何使用这些API-ContactPanel。您也可以检查该示例。