简体   繁体   English

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

[英]Items Data Binded to LongListSelector/Listbox not showing in windows phone 8 local database

I've created a Windows Phone 8 project that adds names to the local SQL CE database.I am adding a single NameItem to the local database in the MainPage.xaml.cs file and it's being added successfully, but it is not showing up in the listbox/longlistselector when running the app. 我创建了一个Windows Phone 8项目,该项目将名称添加到本地SQL CE数据库中。我正在MainPage.xaml.cs文件中将单个NameItem添加到本地数据库中,并且已成功添加它,但未在其中显示运行应用程序时使用列表框/长列表选择器。 I would really appreciate any help. 我真的很感谢您的帮助。 I have the following setup: 我有以下设置:

the Table has the structure shown below: 该表具有如下所示的结构:

 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


}
}

the code for Load.xaml.cs(retrieves data from the local DB): 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; }
}

} }

There doesn't appear to be anything wrong with the binding code. 绑定代码似乎没有任何问题。

Are you sure that PhoneApplicationPage_Loaded_1 is being called? 您确定正在调用PhoneApplicationPage_Loaded_1吗? From the code you have posted, there's no indication that it is . 根据您发布的代码,没有迹象表明它是。

Perhaps you need to add the handler to the loaded event. 也许您需要将处理程序添加到已加载的事件中。 Update the page constructor like so: 像这样更新页面构造函数:

public Load()
{
    InitializeComponent();

    this.Loaded += this.PhoneApplicationPage_Loaded_1;
}

Sample code at: MainPage.xaml & MainPage.xaml.cs 示例代码位于: MainPage.xamlMainPage.xaml.cs

Update based on repro project linked in comments 根据评论中链接的repro项目进行更新

No details were being displayed because there were no records in the database to display. 没有显示任何详细信息,因为数据库中没有要显示的记录。 Getnames() was returning an empty list. Getnames()返回一个空列表。
The reason for this was the database was empty. 原因是数据库为空。

When you are saving the data you are calling InsertOnSubmit() but never submitting the changes and the transaction is never committed to disk. 保存数据时,您正在调用InsertOnSubmit()但从不提交更改,并且事务也从未提交到磁盘。 You need to call ChildDB.SubmitChanges(); 您需要调用ChildDB.SubmitChanges(); after the call to ChildDB.Names.InsertOnSubmit(names); 在调用ChildDB.Names.InsertOnSubmit(names); .

This will save data to the database so when you go to the page to view the details there are details to show and they are displayed on the page. 这会将数据保存到数据库,因此,当您转到页面以查看详细信息时,将显示一些详细信息,并且这些详细信息将显示在页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM