2016-04-25 50 views
0

如果我在'Page1'上有一個列表框(稱爲listBoxobj),它是從本地SQLite數據庫綁定項目,如何從另一個頁面獲取最新項目標識(例如,第2頁)。目前,我有以下幾種方法:如何從另一個頁面獲取最新的列表框項目 - Windows Phone 8

第1頁:

private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (listBoxobj.SelectedIndex != -1) 
    { 
      Refueling listitem = listBoxobj.SelectedItem as Refueling; 
      NavigationService.Navigate(new Uri("/View/EditRefuelingPage.xaml?SelectedContactID=" + listitem.Id, UriKind.Relative)); 
    } 
} 

第2頁:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    Selected_ContactId = int.Parse(NavigationContext.QueryString["SelectedContactID"]); 
    currentrefuel = Db_Helper.ReadRefuel(Selected_ContactId); 
} 

這種方法是從DataBaseHelper類

public Refueling ReadRefuel(int refuelid) 
{ 
    using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
    { 
     var existingrefuel = dbConn.Query<Refueling>("select * from Refueling where Id =" + refuelid).FirstOrDefault(); 
     return existingrefuel; 
    } 
} 

我打電話ReadRefuel()需要另一種方式來獲取最新的物品ID(無需在列表框中單擊它)。我試圖使一個附加的方法在我CRUID類來獲得最新的ID吧:

 public Refueling GetLatestID() 
    { 
      using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
      { 
      var latestID = dbConn.Query<Refueling>("select Id from Refueling").Last(); 
      return latestID; 
      } 
    } 

然後執行以下操作:

   int latestID = Convert.ToInt32(Db_Helper.GetLatestID()); 
       lastRefueling = Db_Helper.ReadRefuel(latestID); 
       oldMileage = Convert.ToDouble(lastRefueling.Mileage); 

,但有一個例外:類型的異常「系統.InvalidCastException」發生在mscorlib.ni.dll但在用戶代碼int latestID = Convert.ToInt32(Db_Helper.GetLatestID());我有型加油的對象沒有被處理

回答

0

其實,讓我不得不採取元素ID從這樣的:lastRefueling = Db_Helper.ReadRefuel(latestID.Id);

相關問題