Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在windows phone c中绑定列表框#_C#_Windows Phone 8 - Fatal编程技术网

C# 如何在windows phone c中绑定列表框#

C# 如何在windows phone c中绑定列表框#,c#,windows-phone-8,C#,Windows Phone 8,在我的windows phone应用程序中,我想获取联系人列表,并遵循此链接http://stackoverflow.com/questions/18387249/selecting-contacts-in-windows-phone-8 工作方式如下: public class CustomContact { public string Name { get; set; } public string Number { get; set; } public CustomCo

在我的windows phone应用程序中,我想获取联系人列表,并遵循此链接
http://stackoverflow.com/questions/18387249/selecting-contacts-in-windows-phone-8
工作方式如下:

public class CustomContact
{
   public string Name { get; set; }
   public string Number { get; set; }

   public CustomContact()
    {
    }


    public CustomContact(Contact contact)
    {
        Name = contact.DisplayName;
        var number = contact.PhoneNumbers.FirstOrDefault();
        if(number != null)
            Number = number.PhoneNumber;
        else
            Number = "";
    }
}
在上面的代码中,我正在创建CustomContact类,下面是xaml.cs页面

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 GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        private void ButtonContacts_Click(object sender, RoutedEventArgs e)
        {
            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

            cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
        }
        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
           // MessageBox.Show(e.Results.Count().ToString());
            try
            {
List<CustomContact> listOfContacts = new List<CustomContact>();
foreach (var c in e.Results)
{
    CustomContact contact  = new CustomContact();
    contact.DisplayName = c.DisplayName;
    var number = c.PhoneNumbers.FirstOrDefault(); //change this to whatever number you want
    if (number != null)
        contact.Number = number.PhoneNumber;
    else
        contact.Number = "";
            }
            catch (System.Exception)
            {
                //No results
            }

            if (ContactResultsData.Items.Any())
            {
                ContactResultsLabel.Text = "results";
            }
            else
            {
                ContactResultsLabel.Text = "no results";
            }
    listOfContacts.Add(contact);
}
ContactResultsData.DataContext = listOfContacts;
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用GetContacts.Resources;
使用Microsoft.Phone.UserData;
命名空间GetContacts
{
公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
//本地化ApplicationBar的示例代码
//BuildLocalizedApplicationBar();
}
私有无效按钮联系\单击(对象发送者,路由目标)
{
Contacts cons=新联系人();
cons.SearchCompleted+=新事件处理程序(联系人\u SearchCompleted);
cons.SearchAsync(String.Empty,FilterKind.None,“Contacts Test#1”);
}
无效联系人搜索已完成(对象发件人、联系人搜索目标e)
{
//Show(例如Results.Count().ToString());
尝试
{
List-listOfContacts=新列表();
foreach(e.Results中的var c)
{
CustomContact=新的CustomContact();
contact.DisplayName=c.DisplayName;
var number=c.PhoneNumbers.FirstOrDefault();//将其更改为您想要的任何数字
如果(数字!=null)
contact.Number=Number.PhoneNumber;
其他的
联系电话号码=”;
}
捕获(系统异常)
{
//没有结果
}
if(ContactResultsData.Items.Any())
{
ContactResultsLabel.Text=“结果”;
}
其他的
{
ContactResultsLabel.Text=“无结果”;
}
联系人列表。添加(联系人);
}
ContactResultsData.DataContext=联系人列表;
xaml页面

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

        <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <Button x:Name="ButtonContacts"
            Content="Get All Contacts"
            FontSize="15"
            Width="200"
            Height="70"
            Background="AliceBlue"
            Foreground="Blue"
            HorizontalAlignment="Left"
            Click="ButtonContacts_Click"></Button>
</Grid>

但是,当我将
listOfContacts
放入列表框
ContactResultsData
时,它不会在列表框中显示联系人,当我将断点放在这一行
ContactResultsData.DataContext=listOfContacts
时,联系人已经存在于
listOfContacts
中,那么为什么它不会显示在列表框
contactresult>中呢tsData
。 请建议我,等待你的答复。
谢谢

我的建议是将
联系人列表
公开给公共财产。同时将其类型更改为
ObservableCollection
。 更改XAML如下:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

        <ListBox Name="ContactResultsData" ItemsSource="{Binding ContactList}" Height="200" Margin="24,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <Button x:Name="ButtonContacts"
            Content="Get All Contacts"
            FontSize="15"
            Width="200"
            Height="70"
            Background="AliceBlue"
            Foreground="Blue"
            HorizontalAlignment="Left"
            Click="ButtonContacts_Click"></Button>
</Grid>


在您的代码中,您应该仅通过删除和添加项目来操作contacts公共属性,而不是更改对它的引用,因为这将破坏XAML和代码之间的绑定。

是否存在DisplayName公共属性?请检查输出窗口中的任何绑定错误。尝试将contacts列表从datacontext更改为t的itemsource他打开了列表框。