2

我需要在 Windows Phone 8 中创建一个列表视图来显示以下调试信息(以下是我当前在 中显示消息的代码Debug.WriteLine)。

        contacts.SearchCompleted += (s, e) =>
        {
            foreach (var contact in e.Results)
            {
                Debug.WriteLine(contact.DisplayName + " - " + contact.PhoneNumbers.First().PhoneNumber);
                textAddressLine1.Text= contact.DisplayName + " - " + contact.PhoneNumbers.First().PhoneNumber;
            }
        };
        contacts.SearchAsync("", FilterKind.DisplayName, null);

你们有没有人在这个平台上创建了一个列表视图并且可以帮助我?

4

1 回答 1

2

这是我能想到的最简单的例子。
我还添加了对没有任何电话号码的联系人的处理。

xml:

<phone:PhoneApplicationPage
    x:Class="so17564250.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="SO 17564250" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="listview example" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector ItemsSource="{Binding}" />
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

CS:

namespace so17564250
{
    using System.Collections.ObjectModel;
    using System.Linq;

    using Microsoft.Phone.Controls;
    using Microsoft.Phone.UserData;

    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            this.DisplayedContacts = new ObservableCollection<string>();

            this.DataContext = this.DisplayedContacts;

            var contacts = new Contacts();

            contacts.SearchCompleted += (s, e) =>
            {
                foreach (var contact in e.Results)
                {
                    this.DisplayedContacts.Add(contact.DisplayName + " - " +
                    (contact.PhoneNumbers.Any()
                        ? contact.PhoneNumbers.First().PhoneNumber
                        : string.Empty));
                }
            };

            contacts.SearchAsync(string.Empty, FilterKind.DisplayName, null);
        }

        public ObservableCollection<string> DisplayedContacts { get; set; }
    }
}
于 2013-07-10T08:45:40.413 回答