异步任务引发 NotImplementedException
Async task raises NotImplementedException
我继续为 Windows Phone 编程而苦恼。 :)
调用方法时:
public static async Task<string> ConvertToBase64(this BitmapImage bitmapImage)
{
RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
var streamWithContent = await rasr.OpenReadAsync();
byte[] buffer = new byte[streamWithContent.Size];
var result = await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
using (MemoryStream ms = new MemoryStream(result.ToArray()))
{
return Convert.ToBase64String(ms.ToArray());
}
}
来自
public async void SetPhotoString(BitmapImage bi)
{
photoBase64 = await bi.ConvertToBase64();
}
in class Offer
来自按钮回调:
public async void Post()
{
var newOffer = new Offer()
{
BookAuthor = Author,
BookTitle = Title,
CurrencyWorth = Price,
Description = Description,
StartedAt = DateTime.UtcNow,
Status = OfferStatus.Added
};
newOffer.SetPhotoString(Photo);//this line throws an excp.
var result = await offerService.AddOffer(newOffer);
if (result != null)
ClearFormula();
}
我得到了 "NotImplementedException" 的支持。我想我在异步调用中做错了什么,但我不知道是什么。
异常详情:
message: The method or operation is not implemented.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Models.Offer.<SetPhotoString>d__0.MoveNext()
更多信息:
照片是我的 AddNewOfferViewModel 的 BitmapImage 属性,它充当图像控件的源。它是从虚拟设备上的相机捕获的:
public async void TakeAPhoto()
{
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("TestPhoto.jpg",
CreationCollisionOption.GenerateUniqueName);
await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, file);
Photo = new BitmapImage(new Uri(file.Path));
await MediaCapture.StopPreviewAsync();
NotifyOfPropertyChange(() => Photo);
ShowCaptureFrame = false;
NotifyOfPropertyChange(() => ShowCaptureFrame);
}
当我将给出异常的行替换为:
newOffer.PhotoBase64 = await Photo.ConvertToBase64();
我遇到异常:
Additional information: Value does not fall within the expected range.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.WindowsPhone.ViewModels.AddOfferViewModel.<Post>d__1.MoveNext()
为什么没有等待您的 ConvertToBase64
代码?这是完全错误的,因为 async void
构造。
尝试将您的代码更改为如下内容:
newOffer.photoBase64 = await SetPhotoString(Photo);//this line throws an excp.
public async Task<string> SetPhotoString(BitmapImage bi)
{
return await bi.ConvertToBase64();
}
此外,ConvertToBase64
方法的哪一行抛出异常?
连续运行是否出现异常?
如果不是,那么您用于 async
代码的某些 类 没有正确实现 GetAwaiter
方法,这会导致您出现异常。
如果是,那么问题出在 Photo
属性 你没有告诉我们。可能是您的代码中有一些未实现的方法。
在您的 ConvertToBase64 扩展方法中尝试 return 任务。
using (MemoryStream ms = new MemoryStream(result.ToArray()))
{
return Task.FromResult(Convert.ToBase64String(ms.ToArray()));
}
我继续为 Windows Phone 编程而苦恼。 :)
调用方法时:
public static async Task<string> ConvertToBase64(this BitmapImage bitmapImage)
{
RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
var streamWithContent = await rasr.OpenReadAsync();
byte[] buffer = new byte[streamWithContent.Size];
var result = await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
using (MemoryStream ms = new MemoryStream(result.ToArray()))
{
return Convert.ToBase64String(ms.ToArray());
}
}
来自
public async void SetPhotoString(BitmapImage bi)
{
photoBase64 = await bi.ConvertToBase64();
}
in class Offer
来自按钮回调:
public async void Post()
{
var newOffer = new Offer()
{
BookAuthor = Author,
BookTitle = Title,
CurrencyWorth = Price,
Description = Description,
StartedAt = DateTime.UtcNow,
Status = OfferStatus.Added
};
newOffer.SetPhotoString(Photo);//this line throws an excp.
var result = await offerService.AddOffer(newOffer);
if (result != null)
ClearFormula();
}
我得到了 "NotImplementedException" 的支持。我想我在异步调用中做错了什么,但我不知道是什么。
异常详情:
message: The method or operation is not implemented.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Models.Offer.<SetPhotoString>d__0.MoveNext()
更多信息: 照片是我的 AddNewOfferViewModel 的 BitmapImage 属性,它充当图像控件的源。它是从虚拟设备上的相机捕获的:
public async void TakeAPhoto()
{
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("TestPhoto.jpg",
CreationCollisionOption.GenerateUniqueName);
await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, file);
Photo = new BitmapImage(new Uri(file.Path));
await MediaCapture.StopPreviewAsync();
NotifyOfPropertyChange(() => Photo);
ShowCaptureFrame = false;
NotifyOfPropertyChange(() => ShowCaptureFrame);
}
当我将给出异常的行替换为:
newOffer.PhotoBase64 = await Photo.ConvertToBase64();
我遇到异常:
Additional information: Value does not fall within the expected range.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at SecondHandBookshop.WindowsPhone.ViewModels.AddOfferViewModel.<Post>d__1.MoveNext()
为什么没有等待您的 ConvertToBase64
代码?这是完全错误的,因为 async void
构造。
尝试将您的代码更改为如下内容:
newOffer.photoBase64 = await SetPhotoString(Photo);//this line throws an excp.
public async Task<string> SetPhotoString(BitmapImage bi)
{
return await bi.ConvertToBase64();
}
此外,ConvertToBase64
方法的哪一行抛出异常?
连续运行是否出现异常?
如果不是,那么您用于 async
代码的某些 类 没有正确实现 GetAwaiter
方法,这会导致您出现异常。
如果是,那么问题出在 Photo
属性 你没有告诉我们。可能是您的代码中有一些未实现的方法。
在您的 ConvertToBase64 扩展方法中尝试 return 任务。
using (MemoryStream ms = new MemoryStream(result.ToArray()))
{
return Task.FromResult(Convert.ToBase64String(ms.ToArray()));
}