C# 无法在静态上下文中访问非静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16904830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:06:19  来源:igfitidea点击:

Cannot access non-static method in static context?

c#wpfmvvm

提问by Isaiah Nelson

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。

采纳答案by Federico Berasategui

If this is WPF, System.Windows.Threading.Dispatcherdoes 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.CurrentDispatcherproperty:

如果你想静态调用它(这是,没有对 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 的引用,而是创建与所述后台线程关联的新 Dispatcher 实例。

A more secure way to access the "UI Thread"'s Dispatcher is via the use of the System.Windows.Application.Currentstatic property:

访问“UI 线程”的 Dispatcher 的一种更安全的方法是使用System.Windows.Application.Current静态属性:

Application.Current.Dispatcher.BeginInvoke(...etc);

回答by Craig Shearer

It's because Dispatcheris a class not a property. Shouldn't you be making your CalibrationViewModelclass a subclass of some other class which has a Dispatcherproperty?

这是因为Dispatcher是一个类而不是一个属性。你不应该让你的CalibrationViewModel类成为其他有Dispatcher属性的类的子类吗?

回答by Mike Perrenoud

Change this:

改变这个:

Dispatcher.BeginInvoke

to this:

对此:

Dispatcher.CurrentDispatcher.BeginInvoke

the issue is BeginInvokeis an instance method and needs an instance to access it. However, your current syntax is trying to access BeginInvokein a staticmanner off the class Dispatcherand that's what's causing this error:

问题是BeginInvoke一个实例方法,需要一个实例来访问它。但是,您当前的语法试图以类外BeginInvokestatic方式访问,Dispatcher这就是导致此错误的原因:

Cannot access non-static method BeginInvoke in static context

无法在静态上下文中访问非静态方法 BeginInvoke