简体   繁体   English

C#中出错:“非静态字段,方法或属性需要对象引用”

[英]Error in C#: “An object reference is required for the non-static field, method, or property”

I wrote code in WPF. 我在WPF中编写代码。 Firstly, I wrote a separate project to test work with a COM port device, and it worked well. 首先,我编写了一个单独的项目来测试使用COM端口设备的工作,它运行良好。 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. 我只是将它复制到一个新的代码文件中。

This code works well: 这段代码效果很好:

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 + '\n');
            }
            catch (Exception ex)
            {
                textBox1.AppendText(ex.Message + '\n');
            }
        }
    }
}

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);
        }
    }
}

Error: 错误:

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#\\PresidentProtocol\\PresidentProtocol\\classes\\QRCodeReader.cs 非静态字段,方法或属性'System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority,System.Delegate,object)'E:\\ C#\\ PresidentProtocol \\ PresidentProtocol需要对象引用\\类\\ QRCodeReader.cs

on this line of code: 在这行代码上:

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

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 . 在第一个代码中,您位于一个继承自Window的类中,因此您在作用域中有一个Dispatcher属性,它返回Dispatcher一个实例。 In the second code, you're in the QRBarCode class, which doesn't have a Dispatcher property; 在第二个代码中,您位于QRBarCode类中,该类没有Dispatcher属性; 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 ; 你需要一个Dispatcher实例来调用Invoke ; you can use the one from the application: 你可以使用应用程序中的那个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误:非静态字段,方法或属性需要对象引用 - C# Error: Object reference is required for the non-static field, method, or property 为什么我在C#中得到“非静态字段,方法或属性需要对象引用”错误? - Why am I getting “object reference is required for the non-static field, method, or property” error in C#? C# 错误:“非静态字段、方法或属性需要对象引用” - C# error: "An object reference is required for the non-static field, method, or property" C#修复错误:“非静态字段,方法或属性需要对象引用” - C# Fixing error: “An object reference is required for the non-static field, method, or property” C#错误(使用接口方法):非静态字段,方法或属性需要对象引用 - C# error(using interface methods): An object reference is required for the non-static field, method, or property C#错误:非静态字段,方法或属性需要对象引用 - C# error: An object reference is required for the non-static field, method, or property 非静态字段,方法或属性错误需要对象引用 - An object reference is required for the non-static field, method or property error 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error C#错误:非静态字段需要对象引用 - C# error : An object reference is required for the non-static field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM