【问题标题】:Flashlight App crashing every time in Windows Phone手电筒应用程序每次在 Windows Phone 中崩溃
【发布时间】:2015-01-10 15:52:37
【问题描述】:

我正在尝试通过 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() 方法,但它也不起作用。 这是一个例外:

“System.Exception”类型的第一次机会异常发生在 mscorlib.ni.dll WinRT 信息:与此错误相关的文本 找不到代码。

  • 当“initializeasync”中的文本高亮显示时显示

【问题讨论】:

  • 有什么异常?
  • “在 mscorlib.ni.dll WinRT 信息中出现了“System.Exception”类型的第一次机会异常:找不到与此错误代码关联的文本。 - 当“initializeasync”中的文本突出显示时显示
  • 考虑编辑帖子以介绍这些细节。
  • 你确定在调用“dispose”之后第二次按钮点击完成了吗?

标签: c# .net dispose flashlight


【解决方案1】:

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/

【讨论】:

    【解决方案2】:

    这个问题可能与多线程有关:使用默认值(即不更改SynchronizationContext)调用await 将继续另一个线程上的方法,图形和媒体库并不总是支持的东西(我有第一手资料)使用 SFML、WPF 和 AutoCAD 的经验变得非常崩溃,仅举几例)。虽然InitializeAsync 方法的存在表明并非如此,但请确保不需要在主线程等上进行处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多