【问题标题】:MediaElement.Source throws 'System.InvalidOperationException'MediaElement.Source 抛出“System.InvalidOperationException”
【发布时间】:2018-03-08 02:18:12
【问题描述】:

我正在编写一个 WPF 应用程序,并且我在一个名为 ViewmodelVideos 的类中有一个名为 VideoPlayerMediaElement-属性。

当我说

ViewModelVideos.VideosPlayer.Source = new Uri(text);

我得到一个例外:

Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll

我无法弄清楚为什么那不起作用。有什么想法吗?

编辑:

"text" 是一个有效的 url,我可以将它放入我的浏览器并且它可以工作。没有 https,简单的 http://...

【问题讨论】:

  • 在视图模型中有一个 MediaElement 听起来很奇怪。视图模型不应该有任何视图元素。除此之外,我的猜测是您正在 UI 线程之外进行调用。异常消息是什么?
  • @Clemens 你能告诉我如何获取异常消息吗?程序没有停止,只有异常消息Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll 出现在输出窗口中。也没有 try catch 。
  • @Clemens 关于在 ui 线程外进行调用的评论似乎是正确的。你知道如何从ui线程外设置源吗?
  • ViewModelVideos.VideosPlayer.Dispatcher.Invoke(() => ViewModelVideos.VideosPlayer.Source = new Uri(text));
  • 在哪里?隐藏代码?

标签: c# wpf mediaelement video-player


【解决方案1】:

@Clemens 评论成功了:

问题是,我在 UI 线程之外进行调用。

解决办法是

ViewModelVideos.VideosPlayer.Dispatcher.Invoke(() => ViewModelVideos.VideosPlayer.Source = new Uri(text)); 

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多