繁体   English   中英

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

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

我创建了一个Windows Phone 8项目,该项目将名称添加到本地SQL CE数据库中。我正在MainPage.xaml.cs文件中将单个NameItem添加到本地数据库中,并且已成功添加它,但未在其中显示运行应用程序时使用列表框/长列表选择器。 我真的很感谢您的帮助。 我有以下设置:

该表具有如下所示的结构:

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

}

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

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

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

public Load()
{
    InitializeComponent();

    this.Loaded += this.PhoneApplicationPage_Loaded_1;
}

示例代码位于: MainPage.xamlMainPage.xaml.cs

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

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

保存数据时,您正在调用InsertOnSubmit()但从不提交更改,并且事务也从未提交到磁盘。 您需要调用ChildDB.SubmitChanges(); 在调用ChildDB.Names.InsertOnSubmit(names);

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

暂无
暂无

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

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