MediaCapture + CaptureElement lifecycle/navigation management

前端 未结 2 1887
走了就别回头了
走了就别回头了 2020-12-19 09:17

This is in a Windows Phone 8.1 Store app. My MainPage has a CaptureElement to display the preview stream from my MediaCapture object.

2条回答
  •  攒了一身酷
    2020-12-19 09:58

    The scenarios you described should trigger the Window.Current.VisibilityChanged event where you can use VisibilityChangedEventArgs.Visible passed into the event handler to cleanup preview when not visible and initialize preview when visible. You can subscribe\unsubscribe to Window.Current.VisibilityChanged event in your Loaded\Unloaded handler for your Page\UserControl.

    The reason why Suspend/Resume lifecycle events is not sufficient is because the scenarios you mentioned above doesn't deterministically invoke those events at a certain time as the OS will only suspend an app based on an internal policy that can change with OS release updates.

    Also as an aside, I would avoid using Navigation handlers and instead rely on Loaded\Unloaded handlers which will allow initialization\cleanup to occur properly if you ever had to move your CaptureElement into its own UserControl as opposed to in a Page and avoids the scenario where WP will call OnNavigatedFrom and not call OnNavigatedTo for suspend\resume (Loaded\Unloaded will always be called in order).

提交回复
热议问题