• <i id='P99Bf'><tr id='P99Bf'><dt id='P99Bf'><q id='P99Bf'><span id='P99Bf'><b id='P99Bf'><form id='P99Bf'><ins id='P99Bf'></ins><ul id='P99Bf'></ul><sub id='P99Bf'></sub></form><legend id='P99Bf'></legend><bdo id='P99Bf'><pre id='P99Bf'><center id='P99Bf'></center></pre></bdo></b><th id='P99Bf'></th></span></q></dt></tr></i><div id='P99Bf'><tfoot id='P99Bf'></tfoot><dl id='P99Bf'><fieldset id='P99Bf'></fieldset></dl></div>
    • <bdo id='P99Bf'></bdo><ul id='P99Bf'></ul>
  • <small id='P99Bf'></small><noframes id='P99Bf'>

    1. <tfoot id='P99Bf'></tfoot>

      <legend id='P99Bf'><style id='P99Bf'><dir id='P99Bf'><q id='P99Bf'></q></dir></style></legend>

        C# 中的错误:“非静态字段、方法或属性需要对象引用"

        时间:2023-11-11
        1. <legend id='xXzMA'><style id='xXzMA'><dir id='xXzMA'><q id='xXzMA'></q></dir></style></legend>

          <small id='xXzMA'></small><noframes id='xXzMA'>

          <tfoot id='xXzMA'></tfoot>
          <i id='xXzMA'><tr id='xXzMA'><dt id='xXzMA'><q id='xXzMA'><span id='xXzMA'><b id='xXzMA'><form id='xXzMA'><ins id='xXzMA'></ins><ul id='xXzMA'></ul><sub id='xXzMA'></sub></form><legend id='xXzMA'></legend><bdo id='xXzMA'><pre id='xXzMA'><center id='xXzMA'></center></pre></bdo></b><th id='xXzMA'></th></span></q></dt></tr></i><div id='xXzMA'><tfoot id='xXzMA'></tfoot><dl id='xXzMA'><fieldset id='xXzMA'></fieldset></dl></div>
            <tbody id='xXzMA'></tbody>
            <bdo id='xXzMA'></bdo><ul id='xXzMA'></ul>

                  本文介绍了C# 中的错误:“非静态字段、方法或属性需要对象引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 WPF 中编写代码.首先,我编写了一个单独的项目来测试使用 COM 端口 设备的工作,然后它运作良好.接下来我决定将其集成到另一个项目中,但出现错误.我没有更改代码;我只是将它复制到一个新的代码文件中.

                  I wrote code in WPF. Firstly, I wrote a separate project to test work with a COM port device, and it worked well. Next I decided to integrate it in another project, but I get an error. I didn't change the code; I am just copied it into a new code file.

                  这段代码运行良好:

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Windows;
                  using System.Windows.Controls;
                  using System.Windows.Data;
                  using System.Windows.Documents;
                  using System.Windows.Input;
                  using System.Windows.Media;
                  using System.Windows.Media.Imaging;
                  using System.Windows.Navigation;
                  using System.Windows.Shapes;
                  using System.IO.Ports;
                  using System.Windows.Threading;
                  
                  namespace WpfApplication2
                  {
                      /// <summary>
                      /// Interaction logic for MainWindow.xaml
                      /// </summary>
                      public partial class MainWindow : Window
                      {
                          public MainWindow()
                          {
                              InitializeComponent();
                              serial.BaudRate = 9600;
                              serial.Handshake = System.IO.Ports.Handshake.None;
                              serial.Parity = Parity.None;
                              serial.DataBits = 8;
                              serial.StopBits = StopBits.One;
                              serial.ReadTimeout = 200;
                              serial.WriteTimeout = 500;
                              serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
                          }
                  
                          SerialPort serial = new SerialPort();
                          private string recieved_data;
                  
                          private delegate void UpdateUiTextDelegate(string text);
                  
                          private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
                          {
                              if (serial.IsOpen)
                              {
                                  try
                                  {
                                      recieved_data = serial.ReadLine();
                                      Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
                                  }
                                  catch (Exception ex)
                                  {
                                      MessageBox.Show(ex.ToString());
                                  }
                              }
                          }
                  
                          private void DisplayText(string code)
                          {
                              textBox1.AppendText(string1);
                          }
                  
                          private void button1_Click(object sender, RoutedEventArgs e)
                          {
                              ListBoxItem lbi = new ListBoxItem();
                              lbi = (ListBoxItem)listBox1.SelectedItem;
                              serial.Close();
                              serial.PortName = "COM" + (string)lbi.Content;
                              try
                              {
                                  serial.Open();
                                  textBox1.AppendText("Device opened at " + serial.PortName + '
                  ');
                              }
                              catch (Exception ex)
                              {
                                  textBox1.AppendText(ex.Message + '
                  ');
                              }
                          }
                      }
                  }
                  

                  但是这个不想工作,我不明白为什么:

                  But this one doesn't want to work, and I can't understand why:

                  using System.IO.Ports;
                  using System.Windows.Threading;
                  using System;
                  using System.Windows;
                  
                  namespace PresidentProtocol
                  {
                      public class QRBarCode
                      {
                         // private SerialPort serial = new SerialPort();
                  
                          public QRBarCode(string com)
                          {
                              serial.BaudRate = 9600;
                              serial.Handshake = System.IO.Ports.Handshake.None;
                              serial.Parity = Parity.None;
                              serial.DataBits = 8;
                              serial.StopBits = StopBits.One;
                              serial.ReadTimeout = 200;
                              serial.WriteTimeout = 500;
                              serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
                              serial.Close();
                              serial.PortName = com;
                              try
                              {
                                  serial.Open();
                              }
                              catch (Exception)
                              {
                                  MessageBox.Show("Error opening COM port.");
                              }
                          }
                  
                          SerialPort serial = new SerialPort();
                          private string recieved_data;
                  
                  
                          private delegate void UpdateUiTextDelegate(string text);
                  
                          private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
                          {
                              if (serial.IsOpen)
                              {
                                  try
                                  {
                                      recieved_data = serial.ReadLine();
                                      Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
                                  }
                                  catch (Exception ex)
                                  {
                                      MessageBox.Show(ex.ToString());
                                  }
                              }
                          }
                  
                          private void DisplayText(string code)
                          {
                              MessageBox.Show(code);
                          }
                      }
                  }
                  

                  错误:

                  非静态字段、方法或属性System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object)"需要对象引用E:C#PresidentProtocolPresidentProtocolclassesQRCodeReader.cs

                  An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object)' E:C#PresidentProtocolPresidentProtocolclassesQRCodeReader.cs

                  在这行代码上:

                  Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
                  

                  推荐答案

                  在第一个代码中,你在一个继承自 Window 的类中,所以你有一个 Dispatcher 范围内的属性,它返回 Dispatcher 的实例.在第二个代码中,您位于 QRBarCode 类中,该类没有 Dispatcher 属性;因此编译器假定您指的是 Dispatcher 类型,并尝试在此类型上调用 Invoke,但由于它不是静态方法,因此无法调用直接在类型上.

                  In the first code, you are in a class that inherits from Window, so you have a Dispatcher property in scope, which returns an instance of Dispatcher. In the second code, you're in the QRBarCode class, which doesn't have a Dispatcher property; so the compiler assumes you're referring to the Dispatcher type, and tries to call Invoke on this type, but since it's not a static method, it can't be called directly on the type.

                  你需要一个Dispatcher的实例来调用Invoke;您可以使用应用程序中的那个:

                  You need an instance of Dispatcher to call Invoke; you can use the one from the application:

                  Application.Current.Dispatcher.Invoke(...);
                  

                  这篇关于C# 中的错误:“非静态字段、方法或属性需要对象引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法将 lambda 表达式转换为类型“System.Delegate",因为它不是委托类型? 下一篇:如何动态创建一个动作&lt;T&gt;在运行时?

                  相关文章

                1. <i id='da9I6'><tr id='da9I6'><dt id='da9I6'><q id='da9I6'><span id='da9I6'><b id='da9I6'><form id='da9I6'><ins id='da9I6'></ins><ul id='da9I6'></ul><sub id='da9I6'></sub></form><legend id='da9I6'></legend><bdo id='da9I6'><pre id='da9I6'><center id='da9I6'></center></pre></bdo></b><th id='da9I6'></th></span></q></dt></tr></i><div id='da9I6'><tfoot id='da9I6'></tfoot><dl id='da9I6'><fieldset id='da9I6'></fieldset></dl></div>

                  <small id='da9I6'></small><noframes id='da9I6'>

                  <legend id='da9I6'><style id='da9I6'><dir id='da9I6'><q id='da9I6'></q></dir></style></legend>

                    <tfoot id='da9I6'></tfoot>
                        <bdo id='da9I6'></bdo><ul id='da9I6'></ul>