0

伙计们,我正在 WPF 应用程序(C#)的媒体元素中播放视频文件。

我现在需要放置一个上下文菜单(即,当您右键单击时,您可能会获得播放/暂停/停止视频的选项)。我做了以上

 ContextMenu contextMenu1 = new ContextMenu();
            MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
            MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
            MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidPlay.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

并使其在“网格”上可用

 //video play pause 
            contextMenu1.Items.Add(vidPlay); contextMenu1.Items.Add(vidPause); contextMenu1.Items.Add(vidStop);

单击这些子程序时的子程序如下

 //play pause routine handlers
        //'me' is the media element,s name
        void PlayVid_Click(object sender, EventArgs e)
        {
            me.Play();
        }

        void PauseVid_Click(object sender, EventArgs e)
        {
            me.Pause();
        }

        void StopVid_Click(object sender, EventArgs e)
        {
            me.Stop();
        }

但是,当单击任何上下文菜单项时,视频不会暂停或停止。

我哪里做错了 ?

4

1 回答 1

1

好吧,伙计们,我想我看到了哪里出错了(感觉很傻)

 ContextMenu contextMenu1 = new ContextMenu();
            MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
            MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
            MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidPlay.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

我粘贴时忘记更正第 2 项和第 3 项中的 vidPlay:/ 更正为:

   ContextMenu contextMenu1 = new ContextMenu();
                MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
                MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPause.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
                MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidStop.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

和一点额外的小费(这对我来说是一个痛苦的几个小时),你可能不得不使用这些线

  //setting behaviour of MediaElement-This allows to INVOKE the play() / pause() / stop() methods appropriately *08 nv 2013*
                me.LoadedBehavior = MediaState.Manual;
                me.UnloadedBehavior = MediaState.Stop;

这样您就不会在调用play(),pause()stop()方法时遇到麻烦

于 2013-11-08T05:44:56.310 回答