项目数据绑定到LongListSelector / Listbox未显示在Windows Phone 8本地数据库中

时间:2013-02-27 08:59:25

标签: c# windows-phone-7 xaml sql-server-ce windows-phone-8

我创建了一个Windows Phone 8项目,它将名称添加到本地SQL CE数据库。我将一个NameItem添加到MainPage.xaml.cs文件中的本地数据库,并且它已成功添加,但它不是在运行应用程序时显示在列表框/ longlistselector中。我真的很感激任何帮助。我有以下设置:

表格的结构如下所示:

 namespace LocalDB
{
 [Table]
 public class Names : INotifyPropertyChanged, INotifyPropertyChanging
{

     [Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, AutoSync = AutoSync.OnInsert)]
     private int id;

     public int F_Id
     {
         get { return id; }
         set
         {
             NotifyPropertyChanging("F_Id");
             id = value;
             NotifyPropertyChanged("F_Id");
         }
     }


     [Column]
     private string f_name;

     public string F_Name
     {
         get { return f_name; }
         set
         {
             NotifyPropertyChanging("F_Name");
             f_name = value;
             NotifyPropertyChanged("F_Name");
         }
     }

     [Column]
     private string l_name;

     public string L_Name
     {
         get { return l_name; }
         set
         {
             NotifyPropertyChanging("L_Name");
             l_name = value;
             NotifyPropertyChanged("L_Name");
         }
     }



     #region Implementation of INotifyPropertyChanged

     public event PropertyChangedEventHandler PropertyChanged;

     private void NotifyPropertyChanged(string propertyName)
     {
         if (PropertyChanged != null)
         {
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
     }
     #endregion

     #region Implementation of INotifyPropertyChanging

     public event PropertyChangingEventHandler PropertyChanging;

     private void NotifyPropertyChanging(string propertyName)
     {
         if (PropertyChanging != null)
         {
             PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
         }
     }
     #endregion


}
}

Load.xaml.cs的代码(从本地数据库中检索数据):

namespace LocalDB
{
public partial class Load : PhoneApplicationPage
{
    private const string Con_String = @"isostore:/names.sdf";
    String fname, lname;

    public Load()
    {
        InitializeComponent();


    }
    public IList<Names> GetNames()
    {
        IList<Names> namesList = null;
        using (NamesDataContext NamesDB = new NamesDataContext(Con_String))
        {
            IQueryable<Names> query = from c in NamesDB.Names select c;
            namesList = query.ToList();

        }

        return namesList;
    }
    private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
        IList<Names> listnames = this.GetNames();
        List<NameItems> nm = new List<NameItems>();
        foreach (Names mynames in listnames)
        {

            fname = mynames.F_Name.ToString();
            lname = mynames.L_Name.ToString();



            nm.Add(new NameItems() { fname = fname, lname = lname });

        }

        namelonglistselector.ItemsSource = nm;
    }

}
}
**the xaml code with LongListSelector:**
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <phone:LongListSelector  Name="namelonglistselector">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    <TextBlock Name="fnametextblock" Text="{Binding fname}"/>
                    <TextBlock  Name="lnametextblock" Text="{Binding lname}"/>
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>
    </Grid>

**the collection class Nameitems.cs**
namespace LocalDB
{
class NameItems
{
    public string fname { get; set; }
    public string lname { get; set; }
}

}

1 个答案:

答案 0 :(得分:1)

绑定代码似乎没有任何问题。

您确定正在调用PhoneApplicationPage_Loaded_1吗?从您发布的代码中,没有迹象表明它是。

也许您需要将处理程序添加到已加载的事件中。像这样更新页面构造函数:

public Load()
{
    InitializeComponent();

    this.Loaded += this.PhoneApplicationPage_Loaded_1;
}

示例代码:MainPage.xaml&amp; MainPage.xaml.cs

根据评论中链接的repro项目进行更新

没有显示任何详细信息,因为数据库中没有要显示的记录。 Getnames()正在返回一个空列表 原因是数据库是空的。

当您保存正在调用InsertOnSubmit()的数据但从不提交更改时,事务永远不会提交到磁盘。致电ChildDB.SubmitChanges();后,您需要致电ChildDB.Names.InsertOnSubmit(names);

这会将数据保存到数据库中,因此当您转到页面查看详细信息时,会显示详细信息并显示在页面上。