c# - 当应用重新获得焦点时重新启动 MediaCapture

标签 c# windows-8 windows-runtime windows-store-apps

在使用 MediaCapture 类 (Windows.Media.Capture) 显示网络摄像头源的 Windows 8 应用程序 (C#) 中,我正在尝试重新启动预览当应用程序丢失然后重新聚焦时(例如,通过单击屏幕左上角的另一个应用程序,然后再次单击返回到我的应用程序)。

我现在尝试重新启动预览的方式是:

Application.Current.Resuming += (sender, o) => StartVideo(video);
Application.Current.Suspending += (sender, args) => StopVideo();

internal async void StartVideo(CaptureElement e)
{
    try
    {
        this.stream = new MediaCapture();
        await this.stream.InitializeAsync();
        e.Source = this.stream;
        await this.stream.StartPreviewAsync();
    }
    catch
    {
        new MessageDialog("Unable to start the video capture.").ShowAsync();
    }
}

internal async void StopVideo()
{
    try
    {
        await stream.StopPreviewAsync();
    }
    catch { }
}

但是,在我上面描述的示例中,ResumingSuspending 事件似乎没有触发。这不是“暂停”应用程序吗?如果是这样,它是什么/我应该注意哪些事件?

或者,我是否应该使用 this.stream.StartRecord... 方法之一,而不是使用长时间运行的“预览”来显示网络摄像头?

编辑:如果我使用 Visual Studio 的暂停/恢复按钮(在“调试位置”工具栏上)手动触发事件,该功能会按预期运行(视频会在应用恢复时重新启动)。

最佳答案

我看到了一些错误:

  • 你应该避免async void;对除事件处理程序之外的所有方法使用 async Task
  • 对于“命令”事件,例如 Suspending,如果您有 async 事件处理程序,请使用 args.SuspendingOperation.GetDeferral 提供的延迟.

关于c# - 当应用重新获得焦点时重新启动 MediaCapture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14552301/

相关文章:

c# - Windows 8 CollectionViewSource ObservableCollection 绑定(bind)不更新

visual-c++ - Windows 8上的Visual C++ 6.0

c# - 单行水平滚动/可滑动的 GridView

windows-8 - 应用程序在后台时似乎关闭

c# - 在 C# 中检测可序列化数据类中的更改

c# - mvvm 解决嵌套模型和 View 模型

c# - 微软图: how to get all groups reusing the token generated through ADAL

c - 如何在 Windows 8 命令提示符下运行和编译 c

javascript - 有关使用 HTML/CSS/JavaScript 编写应用程序的 Windows 8 Metro 资源列表?

c# - CodeContracts - 误报