gpt4 book ai didi

c# - C# : "An object reference is required for the non-static field, method, or property" 错误

转载 作者:IT王子 更新时间:2023-10-29 04:46:23 29 4
gpt4 key购买 nike

我在 WPF 中编写代码。首先,我编写了一个单独的项目来测试 COM port 的工作。设备,并且运行良好。接下来我决定将它集成到另一个项目中,但我得到了一个错误。我没有更改代码;我只是将它复制到一个新的代码文件中。

此代码运行良好:

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

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

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

错误:

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

在这行代码中:

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

最佳答案

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

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

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

关于c# - C# : "An object reference is required for the non-static field, method, or property" 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652767/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com