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

白昼怎懂夜的黑 提交于 2019-11-30 06:29:37

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(...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!