Panel不包含带0参数的构造函数

时间:2014-03-18 01:28:24

标签: c# winforms user-controls naudio

我需要在 Panel1.cs 中的 panel1 中加载用户控件,问题在于 UserControl(AudioPlaybackPanel )包含 ImportingConstructor [ImportMany] IEnumerable<> ),我无法弄清楚我应该在 Form1中有两个参数AudioPlaybackPanel(????)

我得到的错误是:'NAudio.App.AudioPlaybackPanel'不包含带0参数的构造函数

这是 Form1.cs

namespace NAudio.App
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            AudioPlaybackPanel myPanel = new AudioPlaybackPanel(????);
            panel1.Controls.Add(myPanel);
        }
    }
}

这是我的用户控制面板( AudioPlaybackPanel.cs ):

namespace NAudio.App
{
    [Export]
    public partial class AudioPlaybackPanel : UserControl
    {
        private IWavePlayer waveOut;
        private string fileName = null;
        private WaveStream fileWaveStream;
        private Action<float> setVolumeDelegate;

        [ImportingConstructor]
        public AudioPlaybackPanel([ImportMany]IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
        {
            InitializeComponent();
            LoadOutputDevicePlugins(outputDevicePlugins);
        }


        [ImportMany(typeof(IInputFileFormatPlugin))]
        public IEnumerable<IInputFileFormatPlugin> InputFileFormats { get; set; }

        private void LoadOutputDevicePlugins(IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
        {
            comboBoxOutputDevice.DisplayMember = "Name";
            comboBoxOutputDevice.SelectedIndexChanged += new EventHandler(comboBoxOutputDevice_SelectedIndexChanged);
            foreach (var outputDevicePlugin in outputDevicePlugins.OrderBy(p => p.Priority))
            {
                comboBoxOutputDevice.Items.Add(outputDevicePlugin);
            }            
            comboBoxOutputDevice.SelectedIndex = 0;
        }




        void comboBoxOutputDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelOutputDeviceSettings.Controls.Clear();
            Control settingsPanel;
            if (SelectedOutputDevicePlugin.IsAvailable)
            {
                settingsPanel = SelectedOutputDevicePlugin.CreateSettingsPanel();
            }
            else
            {
                settingsPanel = new Label() { Text = "This output device is unavailable on your system", Dock=DockStyle.Fill };
            }
            panelOutputDeviceSettings.Controls.Add(settingsPanel);
        }

        private IOutputDevicePlugin SelectedOutputDevicePlugin
        {
            get { return (IOutputDevicePlugin)comboBoxOutputDevice.SelectedItem; }
        }


// The rest of the code continues from here on...

    }

}

这是界面:

namespace NAudio.App
{
    public interface IOutputDevicePlugin
    {
        IWavePlayer CreateDevice(int latency);
        UserControl CreateSettingsPanel();
        string Name { get; }
        bool IsAvailable { get; }
        int Priority { get; }
    }
}

以防万一,这是其中一个插件:

DirectSoundOutPlugin.cs

namespace NAudio.App
{
    [Export(typeof(IOutputDevicePlugin))]
    class DirectSoundOutPlugin : IOutputDevicePlugin
    {
        private DirectSoundOutSettingsPanel settingsPanel;
        private bool isAvailable;

        public DirectSoundOutPlugin()
        {
            this.isAvailable = DirectSoundOut.Devices.Count() > 0;
        }

        public IWavePlayer CreateDevice(int latency)
        {
            return new DirectSoundOut(settingsPanel.SelectedDevice, latency);
        }

        public UserControl CreateSettingsPanel()
        {
            this.settingsPanel = new DirectSoundOutSettingsPanel();
            return this.settingsPanel;
        }

        public string Name
        {
            get { return "DirectSound"; }
        }

        public bool IsAvailable
        {
            get { return isAvailable; }
        }

        public int Priority
        {
            get { return 3; } 
        }
    }
}

请帮忙!

2 个答案:

答案 0 :(得分:1)

错误并没有说它预计有两个论点......它只是说它不会取0。

构造函数需要一个参数 - IEnumerable<IOutputDevicePlugin>

public AudioPlaybackPanel([ImportMany]IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
{
    ...
}

你需要找到一些实现IOutputDevicePlugin接口的东西并传递它的集合,即使它只是一个空集合。 (将null传递给构造函数将允许它进行编译,但是当你在LoadOutputDevicePlugins中点击循环时会抛出运行时异常。)


考虑到你的问题的更新,这样的事情会让你开始运行(尽管我怀疑通过一个空列表意味着很多):

var myPanel = new AudioPlaybackPanel(new List<DirectSoundOutPlugin>());
panel1.Controls.Add(myPanel);

答案 1 :(得分:1)

值得一提的是,您是否确实需要从NAudio演示中完整复制AudioPlaybackPanel.cs。它有这个构造函数的原因是它试图演示如何使用NAudio的每个IWavePlayer实现。但在普通的实际应用程序中,您只需选择最适合您的应用程序。 e.g。

this.waveOut = new WaveOut();
waveOut.Init(new AudioFileReader("my file.mp3");
waveOut.Play();

因此,如果你想要的只是播放音频文件,那么就不需要在该特定演示中加入插件架构。