0

我正在使用 ContactPicker 类将 Windows 8 People App 集成到我的应用程序中。当点击一个按钮时,我会加载 People App。用户选择了一些朋友,我想在 ListView 中显示他们的姓名、图像和电子邮件地址。

我在显示名称和图像方面没有问题。它没有显示电子邮件地址,而是显示:System.Collections.Generic.List`1[System.String]任何解决方案?

    IReadOnlyList<GetContactDetails> ConDetails;
    public IReadOnlyList<GetContactDetails> ContactDetails
    {
        get { return ConDetails; }
        set { ConDetails = value; }
    }

    List<GetContactDetails> Contact;
    public List<GetContactDetails> SetContacts
    {
        get { return Contact; }
        set { Contact = value; }
    }

    private async void Contacts_Click(object sender, RoutedEventArgs e)
    {
        SetContacts = new List<App1.GetContactDetails>();
        var contactSelector = new Windows.ApplicationModel.Contacts.ContactPicker();
        contactSelector.CommitButtonText = "Pick contact";

        var SelectedContact = await contactSelector.PickMultipleContactsAsync();
        foreach (var item in SelectedContact)
        {
            SetContacts.Add(new GetContactDetails(item));
        }
        ContactList.ItemsSource = SetContacts;
    }

XAML:

<ListView x:Name="ContactList" ItemTemplate="{StaticResource contactTemplate}" HorizontalAlignment="Left" Height="271" VerticalAlignment="Top" Width="585"/>


    <DataTemplate x:Key="contactTemplate">
        <Grid Width="400" Height="200" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="350"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="75"></RowDefinition>
                <RowDefinition Height="75"></RowDefinition>
                <RowDefinition Height="75"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="Name" FontSize="20" FontWeight="Bold"></TextBlock>
            <TextBlock Grid.Column="1"  Grid.Row="0"  Text="{Binding ContactName}" FontWeight="Bold"></TextBlock>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="Mail" FontSize="20" FontWeight="Bold"></TextBlock>
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding ContactEmails}"  FontSize="10" FontWeight="Bold"></TextBlock>
<!--<Image Height="100" Width="100" Grid.Column="1" Grid.Row="1" Source="{Binding ContactImage}" HorizontalAlignment="Left" VerticalAlignment="Center"></Image>-->


        </Grid>
    </DataTemplate>

GetContactDetails 类:

public class GetContactDetails
{
        public string ContactName { get; private set; }
        public BitmapImage ContactImage { get; private set; }
        public Visibility CanShow { get; set; }

        public List<string> PhoneNumbers { get; set; }
        public List<string> ContactEmails { get; set; }

        public GetContactDetails(ContactInformation c)
        {
            PhoneNumbers = new List<string>();
            ContactEmails = new List<string>();

            CanShow = Visibility.Visible;
            ContactName = c.Name;
            if (c.PhoneNumbers.Count > 0)
            {
                foreach (var item in c.PhoneNumbers)
                {
                    PhoneNumbers.Add(item.Value);
                }
            }
            else
            {
                CanShow = Visibility.Collapsed;
            }
            GetContactImage(c);
        }

        async void GetContactImage(ContactInformation Img)
        {
            var imgStream = await Img.GetThumbnailAsync();
            ContactImage = new BitmapImage();
            if (imgStream != null && imgStream.Size > 0)
            {
                ContactImage.SetSource(imgStream);
            }
        }
    }
4

2 回答 2

1

看起来您的代码没有向 ContactEmails 添加信息。你不需要这样的东西吗?

   public List<string> PhoneNumbers { get; set; }
    public List<string> ContactEmails { get; set; }

    public GetContactDetails(ContactInformation c)
    {
        PhoneNumbers = new List<string>();
        ContactEmails = new List<string>();

        CanShow = Visibility.Visible;
        ContactName = c.Name;
        if (c.PhoneNumbers.Count > 0)
        {
            foreach (var item in c.PhoneNumbers)
            {
                PhoneNumbers.Add(item.Value);
            }
        }
        else
        {
            CanShow = Visibility.Collapsed;
        }

        /// Don't you need the below code somewhere?//////
        if (c.Emails.Count > 0)
        {
            foreach (var item in c.Emails)
            {
                ContactEmails .Add(item.Value);
            }
        }

///////////////////////////////////////////////////////////////
        else
        {
            CanShow = Visibility.Collapsed;
        }
        GetContactImage(c);
    }
于 2013-05-29T15:32:20.097 回答
0

您不能将 TextBlock 设置为列表。您需要将 ContactEmails 解析为单独的字符串。或许是这样的:

创建一个名为 ContactEmailString 的属性

    string _contactEmailString;
    public string ContactEmailsString 
    {
      get{
          return _contactEmailString;
         }
      set
      {
         value=_contactEmailString;
         NotifyPropertyChanged("ContactEmailString");
      }
   }

然后在设置联系信息的函数中执行以下操作:

foreach(var item in ContactEmails)
{
   ContactEmailString+=item+";";
}

然后在您的 xaml 中绑定到 ContactEmailString 而不是 ContactEmail。

<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding ContactEmailString}"  FontSize="10" FontWeight="Bold"></TextBlock>
于 2013-05-29T16:01:03.427 回答