c#-Flashlight应用程序每次在Windows Phone中崩溃
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-Flashlight应用程序每次在Windows Phone中崩溃,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3257字,纯文字阅读大概需要5分钟。
内容图文
![c#-Flashlight应用程序每次在Windows Phone中崩溃](/upload/InfoBanner/zyjiaocheng/946/a8f023591cea4ec8ba73da7ea4b8ba37.jpg)
我正在尝试通过Windows Phone应用程序中的TorchControl类来操作手电筒应用程序:
这是我的代码
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera {0} doesn't exist", desiredCamera));
}
async private void Button_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = String.Empty,
VideoDeviceId = cameraID.Id
});
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
tc.Enabled = true;
mediaDev.Dispose();
}
但是问题是,我每次第二次单击按钮时,应用程序都会崩溃.有人告诉我使用mediaDev.Dispose()方法,但它也无法正常工作.
这是例外:
A first chance exception of type ‘System.Exception’ occurred in
mscorlib.ni.dll WinRT information: The text associated with this error
code could not be found.
>突出显示“ initializeasync”中的文本时显示
解决方法:
MediaCapture在重新初始化时将引发异常.要解决此问题,只需确保当您导航回到“相机”页面或单击“相机”按钮时,不要两次初始化MediaCapture.
MediaCapture mediacapture = new MediaCapture();
bool initialized;
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (initialized == false)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
await mediacapture.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
//Selecting Maximum resolution for Video Preview
var maxPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Height > (i2 as VideoEncodingProperties).Height ? i1 : i2);
//Selecting 4rd resolution setting
var selectedPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ElementAt(3);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, selectedPhotoResolution);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, maxPreviewResolution);
// in my .xaml <CaptureElement Name="viewfinder" />
viewfinder.Source = mediacapture;
mediacapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
await mediacapture.StartPreviewAsync();
initialized = true;
}
另外,在导航至其他页面之前或在相机再次开始预览之前,请确保相机停止预览.无需处置MediaCapture.
private async void GoBack_Click(object sender, RoutedEventArgs e)
{
await mediacapture.StopPreviewAsync();
this.Frame.Navigate(typeof(MainPage));
//Not needed
//mediacapture.Dispose();
}
GetCameraID方法归功于Romasz的博客. http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/
内容总结
以上是互联网集市为您收集整理的c#-Flashlight应用程序每次在Windows Phone中崩溃全部内容,希望文章能够帮你解决c#-Flashlight应用程序每次在Windows Phone中崩溃所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。