[英]Cannot access non-static method in static context?
Given this code.... 给定此代码...。
public class CalibrationViewModel : ViewModelBase
{
private FileSystemWatcher fsw;
public CalibrationViewModel(Calibration calibration)
{
fsw = new FileSystemWatcher
{
Path = @"C:\Users\user\Desktop\Path\ToFile\Test_1234.txt",
Filter = @"Test_1234.txt",
NotifyFilter = NotifyFilters.LastWrite
};
fsw.Changed += (o, e) =>
{
var lastLine = File.ReadAllLines(e.FullPath).Last();
Dispatcher.BeginInvoke((Action<string>) WriteLineToSamplesCollection, lastLine); //line that cites error
};
}
private void WriteLineToSamplesCollection(string line)
{
// do some work
}
}
Why am I getting the error, 'Cannot access non-static method BeginInvoke in static context'? 为什么会出现错误“无法在静态上下文中访问非静态方法BeginInvoke”?
I have looked at several other examples on SE and most cite trying to use a field before the object is created as if they were trying to use a non-static field in a static manner, but I don't understand what it is about my code that is invoking the same error. 我看过SE上的其他几个示例,大多数都引用在创建对象之前尝试使用字段,就好像他们试图以静态方式使用非静态字段一样,但是我不了解它的含义。调用相同错误的代码。
Lastly, what can I do to fix this specific issue/code? 最后,我该怎么做才能解决此特定问题/代码?
Update: Fixed title to reflect issue with a 'method' and not a 'property'. 更新:修复了标题,以反映“方法”而非“属性”的问题。 I also added that the class implements ViewModelBase. 我还补充说,该类实现了ViewModelBase。
If this is WPF, System.Windows.Threading.Dispatcher
does not have a static BeginInvoke()
method. 如果这是WPF,则System.Windows.Threading.Dispatcher
没有静态的BeginInvoke()
方法。
If you want to call that statically (this is, without having a reference to the Dispatcher instance itself), you may use the static Dispatcher.CurrentDispatcher
property: 如果要静态调用(这是在没有引用Dispatcher实例本身的情况下),则可以使用静态Dispatcher.CurrentDispatcher
属性:
Dispatcher.CurrentDispatcher.BeginInvoke(...etc);
Be aware though, that doing this from a background thread will NOT return a reference to the "UI Thread"'s Dispatcher, but instead create a NEW Dispatcher instance associated with the said Background Thread. 但是请注意,从后台线程执行此操作不会返回对“ UI线程”的Dispatcher的引用,而是创建与所述后台线程关联的NEW Dispatcher实例。
A more secure way to access the "UI Thread"'s Dispatcher is via the use of the System.Windows.Application.Current
static property: 访问“ UI线程”的分派器的一种更安全的方法是通过使用System.Windows.Application.Current
静态属性:
Application.Current.Dispatcher.BeginInvoke(...etc);
Change this: 更改此:
Dispatcher.BeginInvoke
to this: 对此:
Dispatcher.CurrentDispatcher.BeginInvoke
the issue is BeginInvoke
is an instance method and needs an instance to access it. 问题是BeginInvoke
是实例方法,需要实例才能访问它。 However, your current syntax is trying to access BeginInvoke
in a static
manner off the class Dispatcher
and that's what's causing this error: 但是,您当前的语法正在尝试以static
方式从类Dispatcher
访问BeginInvoke
,这就是导致此错误的原因:
Cannot access non-static method BeginInvoke in static context 无法在静态上下文中访问非静态方法BeginInvoke
It's because Dispatcher
is a class not a property. 这是因为Dispatcher
是类而不是属性。 Shouldn't you be making your CalibrationViewModel
class a subclass of some other class which has a Dispatcher
property? 您不应该将CalibrationViewModel
类设为具有Dispatcher
属性的其他某个类的子类吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.