我的UWP应用程序抛出" UnhandledException"消息:
提供的流编号无效。 PreviewState。
应用程序执行2个操作:
这两项行动完全合情合理。
当我第一次"扫描&#34>时出现问题(使用Zxing管理的相机预览),然后关闭"扫描"预览,打开照片预览页面并旋转手机。 "旋转"引起异常。
我写了一个超级简单的应用程序来重现异常:
MainPage.xaml中
<Button Content="Scan" Click="Scan_Click" />
<Button Content="Photo" Click="Photo_Click" />
MainPage.xaml.cs中
private async void Scan_Click(object sender, RoutedEventArgs e)
{
MobileBarcodeScanner scanner = new MobileBarcodeScanner();
var result = await scanner.Scan();
}
private void Photo_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(PhotoPage));
}
PhotoPage.xaml
<CaptureElement Name="PreviewControl" Stretch="Uniform"/>
PhotoPage.xaml.cs
MediaCapture _mediaCapture;
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
DeviceInformationCollection videoCaptureDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var camera = (from webcam in videoCaptureDevices
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
_mediaCapture = new MediaCapture();
await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = camera.Id });
PreviewControl.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
}
重现错误的步骤如下:
谢谢!
答案 0 :(得分:0)
我在ZXing.Net.Mobile问题页面上找到了解决方案: https://github.com/Redth/ZXing.Net.Mobile/issues/294
https://github.com/Redth/ZXing.Net.Mobile/blob/master/Source/ZXing.Net.Mobile.WindowsUniversal/ScanPage.xaml.cs发布的代码可帮助我解决问题。