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

前端 未结 1 1976
误落风尘
误落风尘 2020-12-30 01:10

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 ge

相关标签:
1条回答
  • 2020-12-30 02:02

    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.

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

    Application.Current.Dispatcher.Invoke(...);
    
    0 讨论(0)
提交回复
热议问题