我正在构建一个Windows Phone 8.1应用程序并使用WinRT我需要从我的照片中捕获一张照片,并且需要将捕获的照片显示为列表。 我写了下面的代码但是我无法运行它并且我收到错误。 任何1可以纠正这个吗?
代码:
private async void BtnCapturePhoto_Click(object sender, RoutedEventArgs e)
{
BtnCapturePhoto.IsEnabled = false;
var photoStorageFile = await cameraCapture.CapturePhoto();
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await photoStorageFile.OpenReadAsync());
PhotoListView.Items.Add(bitmap);
BtnCapturePhoto.IsEnabled = true;
}
错误:
以下是运行应用程序时的错误:
ERROR1
The best overloaded method match for 'Windows.UI.Xaml.Media.Imaging.BitmapSource.SetSourceAsync(Windows.Storage.Streams.IRandomAccessStream)' has some invalid arguments
错误2
Argument 1: cannot convert from 'void' to 'Windows.Storage.Streams.IRandomAccessStream'
错误3
'object' does not contain a definition for 'OpenReadAsync' and no extension method 'OpenReadAsync' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
答案 0 :(得分:0)
问题是photoStorageFile在你的代码中是一个System.Object类型,但它实际上是一个StorageFile,所以我们首先需要向下转换它。
// Get the photoStorageFile as StorageFile not System.Object
StorageFile photoStorageFile = await cameraCapture.CapturePhoto() as StorageFile;
// Now we can properly set the source of the bitmapImage
using (IRandomAccessStream fileStream = await photoStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
bitmap.Source = bitmapImage;
}
此代码应该可以使用