【问题标题】:Does Not Contain A Constructor That Takes 1 Arguments不包含带 1 个参数的构造函数
【发布时间】:2014-02-04 18:01:46
【问题描述】:

我对 C# 有点陌生,当我的串行连接在串行通信操作期间丢失时,我最近遇到了一个接收“无效操作异常”的问题。我试图通过private void port_ErrorReceived(见下文)捕获错误,但我不断收到错误消息,指出“不包含带 1 个参数的构造函数”。

    private void port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
    {
        bool error = false;

        // Check if the comport is closed
        if (!comport.IsOpen)
        { 
                try
                {
                    // Try to open the port
                    comport.Open();
                }
                catch (UnauthorizedAccessException) { error = true; }
                catch (IOException) { error = true; }
                catch (ArgumentException) { error = true; }
                catch (InvalidOperationException) { error = true; }

                if (error) MessageBox.Show(this, "No serial port identified. Please check your connection.", "Serial Connection Lost", MessageBoxButtons.OK, MessageBoxIcon.Stop);
         }     
    }

这里是我调用我的新事件处理程序的地方:

     comport.ErrorReceived += new SerialErrorReceivedEventArgs(port_ErrorReceived);

我在 StackOverflow 上看到过一些类似的帖子,但我不太确定适用于这种情况的内容。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: c# exception-handling invalidoperationexception


    【解决方案1】:

    我想你的意思很简单:

    comport.ErrorReceived += port_ErrorReceived;
    

    或更详细地说:

    comport.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
    

    (但它们是相同的;没有理由不使用第一个版本)

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2012-08-21
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多