pradeep
pradeep

Reputation: 35

i need a list view example program in windows phone 8

I need to create a list view in Windows Phone 8 to display the following debug information (what follows is my code that currently displays messages in 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);

Have any of you created a listview on this platform and can help me?

Upvotes: 2

Views: 3003

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65564

Here's the simplest example I could come up with.
I've also added handling for contacts without any phone numbers.

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

Upvotes: 2

Related Questions