由于 Windows Phone 7 基于 Silverlight,因此需要异步,因此SaveChanges
上下文中没有方法,而是一个BeginSaveChanges
和EndSaveChanges
方法对。你可以这样称呼他们:
private void SaveChanges_Click(object sender, RoutedEventArgs e)
{
// Start the saving changes operation.
svcContext.BeginSaveChanges(SaveChangesOptions.Batch,
OnChangesSaved, svcContext);
}
private void OnChangesSaved(IAsyncResult result)
{
// Use the Dispatcher to ensure that the
// asynchronous call returns in the correct thread.
Dispatcher.BeginInvoke(() =>
{
svcContext = result.AsyncState as NorthwindEntities;
try
{
// Complete the save changes operation and display the response.
WriteOperationResponse(svcContext.EndSaveChanges(result));
}
catch (DataServiceRequestException ex)
{
// Display the error from the response.
WriteOperationResponse(ex.Response);
}
catch (InvalidOperationException ex)
{
messageTextBlock.Text = ex.Message;
}
finally
{
// Set the order in the grid.
ordersGrid.SelectedItem = currentOrder;
}
}
);
}
该示例来自http://msdn.microsoft.com/en-us/library/gg521146(VS.92).aspx。