系统相关
首页 > 系统相关> > c#-Flashlight应用程序每次在Windows Phone中崩溃

c#-Flashlight应用程序每次在Windows Phone中崩溃

作者:互联网

我正在尝试通过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/

标签:dispose,flashlight,c,net
来源: https://codeday.me/bug/20191120/2047230.html