如何在控制台应用程序中的 WPF MediaElement 上播放视频
我有一个引用 WPF dll 的控制台应用程序。我实例化并尝试在 MediaElement 上播放视频,但它没有触发任何事件。这可能意味着它没有播放视频。以下是我编写的代码:
class Program
{
[STAThread]
static void Main(string[] args)
{
var element = new MediaElement();
element.BeginInit();
element.Source = new Uri("Wildlife.wmv", UriKind.RelativeOrAbsolute);
element.EndInit();
element.LoadedBehavior = element.UnloadedBehavior = MediaState.Manual;
element.MediaOpened += new RoutedEventHandler(MediaElement_MediaOpened);
element.Play();
Console.ReadLine();
}
static void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
Console.WriteLine("Media opened");
}
}
我希望在控制台上写入“媒体已打开”。它在 WPF 应用程序中运行良好。我在这里做错了什么?
我正在使用 WPF 4.0
编辑: 请注意,我对视频输出不感兴趣。我知道我可以实例化一个窗口并在其中加载视频,但这不是我想要做的。我只是想了解为什么这段代码不起作用?
注意: 在 WPF 中,如果我在 Window_Load 中执行同一组行而不将 wpf 元素添加到可视化树;我确实让这个事件被解雇了。它与将元素插入可视化树无关。还有其他需要的东西我不确定那是什么。
I have a Console Application that references the WPF dlls. I instantiated and attempted to play a video on MediaElement but it isn't firing any events. Which perhaps means that it is not playing the video. Following is the code I wrote:
class Program
{
[STAThread]
static void Main(string[] args)
{
var element = new MediaElement();
element.BeginInit();
element.Source = new Uri("Wildlife.wmv", UriKind.RelativeOrAbsolute);
element.EndInit();
element.LoadedBehavior = element.UnloadedBehavior = MediaState.Manual;
element.MediaOpened += new RoutedEventHandler(MediaElement_MediaOpened);
element.Play();
Console.ReadLine();
}
static void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
Console.WriteLine("Media opened");
}
}
I expect "media opened" to be written on console. It works fine in a WPF app. What am I doing wrong here?
I'm using WPF 4.0
EDIT: Please note I'm not interested in the video output. I know I can instantiate a window and load video in that but that's not what I want to do. I just want to understand why isn't this piece of code working?
NOTE: In WPF if I execute the same set of lines in Window_Load without adding the wpf element to visual tree; I do get the event fired. Its not about element being plugged in to visual tree. There is something else that is required I'm not sure what that is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MediaElement
控件需要 Win32 消息循环才能执行其操作。没有它就根本行不通。默认情况下,您的控制台应用程序中不会有一个。它在
Window.Load
事件中起作用的原因是因为有一个消息循环作为 WPF 应用程序的一部分运行。这与“植根于视觉树”无关。这也是为什么 @mzabsky 在 PowerShell 中的解决方案有效,因为
Window.ShowDialog
方法将确保存在消息循环来处理 Win32 消息。The
MediaElement
control requires a Win32 message loop in order to perform its operations. Without one it simply will not work. You will not have one in your console application by default.The reason why it works in your
Window.Load
event is because there is a message loop running as part of the WPF Application. This is independent of the "rooting in the visual tree".This is also why @mzabsky's solution in PowerShell works, as the
Window.ShowDialog
method will ensure a message loop exists to handle the Win32 messages.此是有关如何直接从 PowerShell 控制台打开普通 WPF 窗口的教程。我认为带有 C# 代码的常规控制台的行为几乎相同。
有趣的部分是:
您必须打开一个新窗口作为对话框(否则我猜您必须以某种方式在单独的线程上运行它)。
如果您不想看到窗口,您可以尝试在 ShowDialog 调用后隐藏它,但我认为您无论如何都无法使用控制台。
This is a tutorial on how to open normal WPF window directly from PowerShell console. I suppose regular console with C# code behaves pretty much the same.
The interesting part is this:
You have to open a new window as dialog (otherwise you would have to somehow run it on separate thread, I guess).
If you don't want to see a window, you may try hiding it after the ShowDialog call, but I don't think you will be able to work with the console anyways.