我继续努力使用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();
}
来自按钮回调的课程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()
更多信息: Photo是我的AddNewOfferViewModel的BitmapImage属性,它充当Image控件的源。它是从虚拟设备上的摄像头捕获的:
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()
答案 0 :(得分:0)
为什么您的代码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
属性中,您没有向我们展示。可能是您的代码中有一些未实现的方法。
答案 1 :(得分:0)
尝试在ConvertToBase64扩展方法中返回一个任务。
curl 'http://localhost:8983/solr/update/json?commit=true' --data-binary [URL] -H 'Content-type:application/json'