简体   繁体   中英

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. 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):

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? 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

Update based on repro project linked in comments

No details were being displayed because there were no records in the database to display. Getnames() was returning an empty list.
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. You need to call ChildDB.SubmitChanges(); after the call to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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