我在一个Windows Phone 7应用程序中有一个页面,用户可以编辑或删除Transaction对象。 Transaction对象是一个Linq-to-Sql类,与Account类和Category类有关系。 在页面中,我使用ListPicker让用户选择给定交易的帐户和类别,如下所示:
<toolkit:ListPicker Grid.Row="1" FullModeHeader="Choose the Account" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,0,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,10,0" Name="Category" SelectedItem="{Binding Category, Mode=TwoWay}" Tap="ListPicker_Tap" />
ListPicker_Tap事件是针对Windows Phone的WPF Toolkit 2011年8月版本中一个错误的修复方法,它只是这样的:
private void ListPicker_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ListPicker lp = (ListPicker)sender;
lp.Open();
}
如果用户编辑交易,一切都正常,但是如果用户试图删除它,我会收到一个错误,提示“SelectedItem必须始终设置为有效值”。以下是当用户在TransactionPage.xaml.cs中的应用栏中单击删除按钮时的代码:
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方法:
public void DeleteTransaction(Transaction transaction)
{
AllTransactions.Remove(transaction);
transactionRepository.Delete(transaction);
}
我的transactionRepository.Delete方法:
public void Delete(Transaction transaction)
{
Context.Transactions.DeleteOnSubmit(transaction);
Context.SubmitChanges();
}
在执行Context.SubmitChanges()时,我收到了错误提示,调试指向Transaction类中的NotifyPropertyChanged,发生错误的行是这行:
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在propertyName属性中,它的值是"Category"。看起来当删除对象时,会发送类别和账户的propertychanged事件,由于列表选择器处于双向模式,它在处理时遇到了一些麻烦。我该怎么修复它?我需要一些帮助。
{Binding Account, Mode=TwoWay}
和{Binding Category, Mode=TwoWay}
所指的内容即可。 - Amry