C# 不使用列表框中的SelectedIndex在ItemPage上显示信息 问题

C# 不使用列表框中的SelectedIndex在ItemPage上显示信息 问题,c#,windows-phone-8,azure-mobile-services,C#,Windows Phone 8,Azure Mobile Services,我试图显示从列表框中选择的特定项目的信息。这些项目来自Azure Mobile Services SQL数据库。我试图捕获SelectedIndex,然后查询RowID等于SelectedIndex的数据库。不幸的是,用户可以选择对数据进行排序,从而将项目的ID更改为不再与数据库中的RowID匹配的位置 来自Main.xaml的代码 在Main.xaml页面上,当从Main列表框中选择一个项目时,SelectedItem属性将传递到item.xaml页面,如上所示。然后用于查询数据库。这是正确的

我试图显示从
列表框中选择的特定项目的信息。这些项目来自Azure Mobile Services SQL数据库。我试图捕获
SelectedIndex
,然后查询
RowID
等于
SelectedIndex
的数据库。不幸的是,用户可以选择对数据进行排序,从而将项目的ID更改为不再与数据库中的
RowID
匹配的位置

来自Main.xaml的代码 在
Main.xaml
页面上,当从
Main列表框
中选择一个项目时,
SelectedItem
属性将传递到item.xaml页面,如上所示。然后用于查询数据库。这是正确的吗

编辑2
不要捕获
SelectedIndex
,而是获取
SelectedItem
,它应该是一个具有属性
FirstName
LastName
的对象(可能是您定义的某个类)。从那里,您可以使用这些值查询数据库。类似于下面的代码:

var listItem = MainListBox.SelectedItem as Person;
var table = MobileService.GetTable<Person>();
var serverItem = (await table
   .Where(p => p.FirstName == listItem.FirstName && p.LastName == listItem.LastName)
   .ToEnumerableAsync())
   .FirstOrDefault();
var listItem=MainListBox.SelectedItem为Person;
var table=MobileService.GetTable();
var serverItem=(等待表
.Where(p=>p.FirstName==listItem.FirstName&&p.LastName==listItem.LastName)
.ToEnumerableAsync())
.FirstOrDefault();

我会把它改成这样

App.xaml.cs

public static Phones SelectedPhone { get; set; }
挑选

private void MainListBox_SelectionChanged(object sender,  SelectionChangedEventArgs e)
{
    App.SelectedPhone = MainListBox.SelectedItem as Phones;
    NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml, UriKind.Relative));
}
导航到

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    var listItem = App.SelectedPhone;
    progressBar1.IsEnabled = true;
    progressBar1.IsIndeterminate = true;

    //Query database
    try
    {
        items = await phoneTable
                .Where(phone => phone.Model == listItem.Model)
                .ToCollectionAsync();
     }
     catch (MobileServiceInvalidOperationException)
     {
         MessageBox.Show("Error", "Error", MessageBoxButton.OK);
     }

     MainListBox.ItemsSource = items;

     //End progressBar
     progressBar1.IsEnabled = false;
     progressBar1.Visibility = System.Windows.Visibility.Collapsed;
     progressBar1.IsIndeterminate = false;
}

好的,这是我的代码副本。我不断收到一个错误,上面写着“对象引用未设置为对象的实例”。我在上面对其进行了更新。如果您正在OnNavigatedTo事件上运行代码,则列表可能未选择任何项目,因此,
listItem
很可能为空。@carlosfiguera那么该值应该从上面的编辑2中显示的
Main.xaml
传递给它。您是判断该值是否存在的最佳人选-您的调试器上有该信息。当您得到NullReferenceException时,它发生在哪一行?哪个对象为空?[ExceptionMessage]:[Input string格式不正确][StackTrace]:[System.Number.StringTonNumber(string str,NumberStyles选项,NumberBuffer&Number,NumberFormatInfo,Boolean parseDecimal)在System.Convert.ToInt32的System.Number.ParseInt32(string s,NumberStyles样式,NumberFormatInfo信息)中(字符串值)位于WPTracker.ViewModels.Phones.PhoneItemView.d__2.MoveNext()---从引发异常的上一个位置开始的堆栈结束跟踪---位于System.Runtime.CompilerServices.AsynchMethodBuilderCore.b__0(对象状态)]我已根据您的建议更新了代码,但现在我收到了此错误。
无法将类型“object”隐式转换为“WPTracker.ViewModels.Phones.Phones”。存在显式转换(是否缺少转换?)
你能告诉我是哪个功能或哪个代码行导致的吗?如果是这一行App.SelectedPhone=MainListBox.SelectedItem;那么只需将其更改为App.SelectedPhone=MainListBox.SelectedItem作为电话;我将
App.SelectedPhone=MainListBox.SelectedItem;
更改为``App.SelectedPhone=MainListBox.SelectedItem作为电话“一个一个,”他说,“而且成功了。非常感谢你!
public static Phones SelectedPhone { get; set; }
private void MainListBox_SelectionChanged(object sender,  SelectionChangedEventArgs e)
{
    App.SelectedPhone = MainListBox.SelectedItem as Phones;
    NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml, UriKind.Relative));
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    var listItem = App.SelectedPhone;
    progressBar1.IsEnabled = true;
    progressBar1.IsIndeterminate = true;

    //Query database
    try
    {
        items = await phoneTable
                .Where(phone => phone.Model == listItem.Model)
                .ToCollectionAsync();
     }
     catch (MobileServiceInvalidOperationException)
     {
         MessageBox.Show("Error", "Error", MessageBoxButton.OK);
     }

     MainListBox.ItemsSource = items;

     //End progressBar
     progressBar1.IsEnabled = false;
     progressBar1.Visibility = System.Windows.Visibility.Collapsed;
     progressBar1.IsIndeterminate = false;
}