0

我最近开始研究 C#,我很业余,请在下面描述的问题中提供任何帮助,这对我很有帮助

我想做的是在事件处理程序中也可以访问 video_panel 但现在我不知道该怎么做我试图将媒体元素声明为全局并将其值设置为 video_panel 但仍然没有用我应该做什么我想要而不是显示消息暂停当前正在播放的视频

namespace Play_pause
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

  static   int index = 0;
  static  System.Timers.Timer aTimer = new System.Timers.Timer();
    public MainWindow()
    {
        InitializeComponent();
       video_panel.Play();

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;

        aTimer.Start();




    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {



        index++;
        if (index %2==0)
        {
            MessageBox.Show("Can't access media element in the event handler/n how to    
            set it global?");




        }

    }    
}

}

4

1 回答 1

1

再次阅读这里的答案,它们展示了如何使变量成为全局变量。

But this wont solve your problem. Since your OnTimedEvent-method is static, you can access only static variables. I dont think your OnTimedEvent should be static, so try to remove the word "static" and now you can acess global variables (e.g. your video_panel).

于 2012-10-02T12:43:27.830 回答