C# 异步方法中的错误
在WinRT应用程序中,我有一个带有一些图片的FlipView myFlipView和一个图像myImage。在myFlipView的事件选择更改中,有以下方法:C# 异步方法中的错误,c#,windows-8,windows-runtime,async-await,winrt-async,C#,Windows 8,Windows Runtime,Async Await,Winrt Async,在WinRT应用程序中,我有一个带有一些图片的FlipView myFlipView和一个图像myImage。在myFlipView的事件选择更改中,有以下方法: async private void myFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (myFlipView == null) return; Uri newUri = new Uri("
async private void myFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myFlipView == null) return;
Uri newUri = new Uri("ms-appx://" + (((BitmapImage)(((Image)(((ContentControl)(myFlipView.SelectedItem)).Content)).Source)).UriSource.AbsolutePath));
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(newUri);
WriteableBitmap wb = new WriteableBitmap(1, 1);
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
await wb.SetSourceAsync(fileStream);
}
}
wb = ModifyPicture(wb);
myImage.Source = wb;
}
总之,它在myFlipView中查找当前图像的uri,并在myImage中设置该图像,但在ModifyPicture中定义了一些修改。它在平板电脑上工作得很好,但在有鼠标的电脑上有一个错误。当我快速单击FlipView上的箭头时,我的图像显示错误的图片。例如,如果在myFlipView中我有10张图片(p1,p2,…,p10),并且当前选择了p1,当我在myImage上更改为p2时,p2也会出现。但当我在FlipView中快速点击时,我会看到例如p9和myImage p8。我想这和这个方法被多次调用有关,但我不知道如何修复它。提前感谢您的帮助:)如果事件处理程序在完成之前再次调用,您可能应该保存已经运行的任务/IAsyncOperation并取消它 伪代码(我不知道C#):
除了或代替Mau il提到的取消内部任务之外,如果您检测到异步方法的执行应该被取消,那么您可以中断/取消它。例如
private int myFlipView_SelectionChangedCallId;
async private void myFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myFlipView == null) return;
var callId = ++myFlipView_SelectionChangedCallId;
Uri newUri = new Uri("ms-appx://" + (((BitmapImage)(((Image)(((ContentControl)(myFlipView.SelectedItem)).Content)).Source)).UriSource.AbsolutePath));
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(newUri);
if (callId != myFlipView_SelectionChangedCallId) return;
WriteableBitmap wb = new WriteableBitmap(1, 1);
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
await wb.SetSourceAsync(fileStream);
if (callId != myFlipView_SelectionChangedCallId) return;
}
}
wb = ModifyPicture(wb);
myImage.Source = wb;
}
另外,如果您的ModifyPicture方法进行了任何繁重的像素处理,您可能希望在后台线程上运行它并等待它。您能给我整个项目,以便我检查吗?我使用了Filip Skakun解决方案,现在它工作正常。谢谢你的帮助:)
private int myFlipView_SelectionChangedCallId;
async private void myFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myFlipView == null) return;
var callId = ++myFlipView_SelectionChangedCallId;
Uri newUri = new Uri("ms-appx://" + (((BitmapImage)(((Image)(((ContentControl)(myFlipView.SelectedItem)).Content)).Source)).UriSource.AbsolutePath));
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(newUri);
if (callId != myFlipView_SelectionChangedCallId) return;
WriteableBitmap wb = new WriteableBitmap(1, 1);
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
await wb.SetSourceAsync(fileStream);
if (callId != myFlipView_SelectionChangedCallId) return;
}
}
wb = ModifyPicture(wb);
myImage.Source = wb;
}