我在
Windows Phone 7应用程序中有一个页面,用户可以在其中编辑或删除一个Transaction对象. Transaction对象是一个与Account类和Category类有关系的Linq-to-sql类.在页面中,我使用ListPicker让用户选择给定事务的帐户和类别,如下所示:
<toolkit:ListPicker Grid.Row="1" FullModeHeader="Choose the Account" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,10,0" Name="Account" SelectedItem="{Binding Account,Mode=TwoWay}" Tap="ListPicker_Tap" /> <toolkit:ListPicker Grid.Row="7" FullModeHeader="Choose the Category" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,0" Name="Category" SelectedItem="{Binding Category,Mode=TwoWay}" Tap="ListPicker_Tap" />
ListPicker_Tap事件是针对Windows Phone的WP / WPF工具包的8月/ 2011版本的错误的修复,只是这样:@H_502_5@
private void ListPicker_Tap(object sender,System.Windows.Input.GestureEventArgs e) { ListPicker lp = (ListPicker)sender; lp.Open(); }
如果用户编辑事务,一切都很好,但是如果用户尝试删除它,我会收到一个错误,指出“SelectedItem必须始终被设置为有效的值”.@H_502_5@
如果用户点击TransactionPage.xaml.cs中的应用程序栏中的删除按钮,则代码如下:@H_502_5@
private void appBarDelete_Click(object sender,EventArgs e) { MessageBoxResult result = MessageBox.Show("Are you sure?\n","Confirm",MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { App.viewmodel.DeleteTransaction(transaction); } NavigationService.GoBack(); }
我的viewmodel.DeleteTransaction方法:@H_502_5@
public void DeleteTransaction(Transaction transaction) { AllTransactions.Remove(transaction); transactionRepository.Delete(transaction); }
我的transactionRepository.Delete方法:@H_502_5@
public void Delete(Transaction transaction) { Context.Transactions.DeleteOnSubmit(transaction); Context.SubmitChanges(); }
我在Context.SubmitChanges()执行中收到错误,调试指向Transaction类里面的NotifyPropertyChanged,我得到错误的行是这样的:@H_502_5@
protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } }
在propertyName属性中,值为“Category”.看起来像删除对象时发送categorychanged的propertychanged事件,并且由于listpicker是TwoWay模式,所以处理它有一些麻烦.我该如何解决?我需要一些帮助.@H_502_5@