简体   繁体   English

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

[英]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.

相关问题 在非静态上下文中无法访问静态方法 - Cannot access static method in a non-static context 无法在 static 上下文中访问非静态方法“IsMatch” - Cannot access non-static method 'IsMatch' in static context 无法在静态上下文中访问非静态字段“lblSystemStatus” - Cannot access non-static field 'lblSystemStatus' in static context 在静态上下文中无法访问非静态字段 - Cannot access non-static field in static context 嵌套类:无法访问静态上下文中的非静态字段 - Nested class: Cannot access non-static field in static context C#编译器:无法在非静态上下文中访问静态方法 - C# Compiler : cannot access static method in a non-static context C#无法在静态上下文中访问非静态成员字段,除非实际处于静态上下文中 - C# Cannot access non-static member field in static context, without actually being in static context 无法通过APM扩展访问TAP中的非静态方法 - Cannot access non-static method in TAP over APM extension 使用依赖属性解决“无法访问 static 上下文中的非静态属性”的另一种方法 - An alternative way around 'Cannot access non-static property in static context' using dependency property 为什么我在使用动态时能够在静态上下文中访问非静态方法 - Why am I able to access a non-static method in a static context when using dynamic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM