各位大神,能不能帮我解决一个问题。就是在C#中有个控件叫WindowsMediaPlayer这个控件。我在Form1窗口中加入了axWindowsMediaPlayer1、axWindowsMediaPlayer2、axWindowsMediaPlayer3;三个控件。这个控件有播放暂停和停止功能。那么我想封装一个类,就是比如说我封装了一个bofang类,那么我在调用时就是希望通过调用这个类来实现播放器的播放、停止、暂停功能。现在就是不知道怎么封装。怎么定义变量,怎么定义方法。急啊,求各位大神帮忙。
axWindowsMediaPlayer1.Ctlcontrols.play();//播放功能
axWindowsMediaPlayer1.Ctlcontrols.pause();//暂停功能
axWindowsMediaPlayer1.Ctlcontrols.stop();//停止功能
也就是说正常我要写这三行代码,那么在axWindowsMediaPlayer2中我又要写这三行,我就是要将他们封装成一个,然后三个播放器都可调用。希望能解决这种问题,这样我以后封装别的就会了。

解决方案 »

  1.   

    http://blog.csdn.net/neusoft06/article/details/8934407
    参考一下
      

  2.   

    新建一个Players类,
    private List<axWindowsMediaPlayer> _players;
    在构造函数中通过参数指定需要的数量
    public Players(int num)
    {
        this._players = new List<axWindowsMediaPlayer>(num);
        for(int i=0; i<num; i++) 
        {
             this._players.Add(new axWindowsMediaPlayer());
        }
    }public void Action(Action<axWindowsMediaPlayer> action)
    {
        this._players.Foreach(player => action(player));
    }类似的定义一个Remove和Clear()之类的释放对象的方法
    在Form2中声明一个
    private Players _players;public Form2()
    {
        this._players = new Players(3);
    }操作的时候调用一下
    this._players.Action(player => player.Ctlcontrols.play());