C# 联系人创建问题

C# 联系人创建问题,c#,windows-phone-8.1,C#,Windows Phone 8.1,我只是想用下面的代码创建简单的联系人。没有编译问题,我可以在输出窗口中看到正在创建的联系人,但由于某些原因,联系人未在手机的联系人存储中创建。我是不是遗漏了什么 这里是我得到remoteHelper类的地方 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using Sy

我只是想用下面的代码创建简单的联系人。没有编译问题,我可以在输出窗口中看到正在创建的联系人,但由于某些原因,联系人未在手机的联系人存储中创建。我是不是遗漏了什么

这里是我得到remoteHelper类的地方

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using EmulateContacts.Resources;
using System.IO;
using Windows.Phone.PersonalInformation;
using System.Xml.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage.Streams;

namespace EmulateContacts
{

    public class RemoteContact
    {
        public string RemoteId { get; set; }
        public string GivenName { get; set; }
        public string FamilyName { get; set; }
        public string DisplayName { get; set; }
        public string Email { get; set; }
        public string CodeName { get; set; }
        public Stream photo { get; set; }

        public override string ToString()
        {
            return String.Format(" {0}\n {1}\n {2}\n {3}\n {4}\n {5}", RemoteId, GivenName, FamilyName, DisplayName, Email, CodeName);
        }
    }

    public partial class MainPage : PhoneApplicationPage
    {
        ContactStore contactStore;
        RemoteIdHelper remoteIdHelper;

        public MainPage()
        {
            InitializeComponent();
        }

        private void btnAddContacts_Click(object sender, RoutedEventArgs e)
        {
            StatusTextBlock.Text = "Importing contacts...";

            CreateContacts();

            StatusTextBlock.Text = "Done.";
        }

        private async void CreateContacts()
        {

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

                remoteIdHelper = new RemoteIdHelper();

                await remoteIdHelper.SetRemoteIdGuid(contactStore);

                int remoteId = 0;
                int DN = 0;
                int mail = 0;
                int n;
                n = 10;

                for (int i = 0; i < n; i++)
                {
                    Stream myStream = App.GetResourceStream(new Uri(@"Sample.jpg", UriKind.RelativeOrAbsolute)).Stream;
                    var remoteContact = new RemoteContact
                    {
                        RemoteId = remoteId.ToString(),
                        GivenName = "S",
                        FamilyName = "BCB",
                        DisplayName = "CR" + DN,
                        Email = "Sync" + mail + "@gmail.com",
                        CodeName = "R",
                        photo = myStream,

                    };
                    remoteId++; DN++; mail++;

                    await AddContact(remoteContact);
                }
         }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }

        public async Task AddContact(RemoteContact remoteContact)
        {
            // Create a new StoredContact object
            StoredContact contact = new StoredContact(contactStore);

            // Make sure the remote contact has a RemoteId value
            if (remoteContact.RemoteId == null)
            {
                return;
            }

            // Use the RemoteIdHelper class to add a GUID to the remote ID to make sure
            // the value is unique to this app
            contact.RemoteId = await remoteIdHelper.GetTaggedRemoteId(contactStore, remoteContact.RemoteId);

            // Set the properties that are exposed directly by the StoredContact class
            if (remoteContact.GivenName != null) contact.GivenName = remoteContact.GivenName;
            if (remoteContact.FamilyName != null) contact.FamilyName = remoteContact.FamilyName;

            // If you don't supply a display name, "[GivenName] [FamilyName]" will be used, but it won't
            // automatically be updated when you update GivenName or FamilyName.
            if (remoteContact.DisplayName != null) contact.DisplayName = remoteContact.DisplayName;

            IInputStream I = remoteContact.photo.AsInputStream();
            await contact.SetDisplayPictureAsync(I);


            // Call GetPropertiesAsync to get the dictionary of properties that are understood by the phone. 
            // The keys for this dictionary must be values from the KnownContactProperies enumeration.
            IDictionary<string, object> props = await contact.GetPropertiesAsync();
            if (remoteContact.Email != null) props.Add(KnownContactProperties.Email, remoteContact.Email);

            // Call GetPropertiesAsync to get the dictionary of properties that are specific to your app.
            // In this case, the app will set a CodeName property.
            IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync();
            if (remoteContact.CodeName != null) extprops.Add("CodeName", remoteContact.CodeName);

            try
            {
                // Call SaveAsync to save the contact into the store.
                await contact.SaveAsync();
                Debug.WriteLine(String.Format("Adding:\n{0}", remoteContact.ToString()));
            }
            catch (Exception)
            {
                Debug.WriteLine(String.Format("Unable to add contact: {0}", remoteContact.ToString()));
            }
        }

    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用EmulateContacts.Resources;
使用System.IO;
使用Windows.Phone.PersonalInformation;
使用System.Xml.Linq;
使用系统诊断;
使用System.Threading.Tasks;
使用Windows.Storage.Streams;
命名空间模拟联系人
{
公共类远程联系人
{
公共字符串RemoteId{get;set;}
公共字符串GivenName{get;set;}
公共字符串FamilyName{get;set;}
公共字符串DisplayName{get;set;}
公共字符串电子邮件{get;set;}
公共字符串代码名{get;set;}
公共流照片{get;set;}
公共重写字符串ToString()
{
返回String.Format(“{0}\n{1}\n{2}\n{3}\n{4}\n{5}”,RemoteId,GivenName,FamilyName,DisplayName,Email,CodeName);
}
}
公共部分类主页:PhoneApplicationPage
{
通讯商店;
RemoteIdHelper RemoteIdHelper;
公共主页()
{
初始化组件();
}
私有无效btnAddContacts_单击(对象发送者,路由目标e)
{
StatusTextBlock.Text=“导入联系人…”;
CreateContacts();
StatusTextBlock.Text=“完成。”;
}
私有异步void CreateContacts()
{
尝试
{ 
contactStore=等待contactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite,ContactStoreApplicationAccessMode.ReadOnly);
remoteIdHelper=新的remoteIdHelper();
等待remoteIdHelper.SetRemoteIdGuid(contactStore);
int-remoteId=0;
int DN=0;
int mail=0;
int n;
n=10;
对于(int i=0;i
您的联系人可能已创建但不可见,因为Windows Phone 8.1对8.0进行了重大更改-默认情况下不会显示来自第三方应用程序的联系人。转到People应用程序,在第一页(联系人)的顶部,您将看到“显示全部”/“显示一些帐户”。点击该按钮可管理指定联系人的可见性