c#-4.0 - 我需要 Windows Phone 8 中的 ListView 示例程序

标签 c#-4.0 windows-phone-8

我需要在 Windows Phone 8 中创建一个 ListView 来显示以下调试信息(下面是我当前在 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);

你们中有人在这个平台上创建了 ListView 并且可以帮助我吗?

最佳答案

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

xaml:

<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; }
    }
}

关于c#-4.0 - 我需要 Windows Phone 8 中的 ListView 示例程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564250/

相关文章:

sqlite - 如何忽略删除时的外键限制?

Windows 应用商店应用着色器模型 4_0_level_9_3 和 VPOS

c# - 如何从 SQLite 的异步 PCL 版本使用 SQLiteAsyncConnection?

c# - 带有 "validateAllProperties"的 Validator.ValidateObject 在第一次错误时真正停止

selenium - pagefactory 在 C# 中显式等待 Web 元素的集合

testing - 当我们可以做一个委托(delegate)使其可测试时,为什么要提取一个接口(interface)?

windows-phone-7 - Windows Phone 7 应用程序在 Windows Phone 8 上的顶部边距问题

c# - 单击母版中的按钮时更改母版页

c# - 异步 JSON 反序列化

c# - 从 StackPanel 中删除第一个项目以外的所有项目?