小编nov*_*der的帖子

C# 关键字“this”在当前上下文中不可用

我有一个接口 Service,它有一个方法 GoTo

 public interface Service
 {
   bool GoTo(string NextPage , object parameter);
 }
Run Code Online (Sandbox Code Playgroud)

我有一个A类如下

 public class A
 {
    private Service serviceA;

   //constructor of the class
   public A(Service serviceB, ServiceManager serviceManager)
   {
      this.serviceA = serviceB;
   }
   public ObservableCollection<SampleObject> Example { get; } = new 
   ObservableCollection<SampleObject>()
    {
        new SampleObject() { Action=  new DelegateCommand(() => this.serviceA.GoTo("NextPage", null)) 
    },
     
    };
Run Code Online (Sandbox Code Playgroud)

}

我收到一个错误,说关键字“this”在当前上下文中不可用。我查看了字段初始值设定项无法引用非静态字段、方法或属性,我可以看到该错误是由于变量未在构造函数中初始化,但在我的情况下,我这样做了。任何帮助理解这一点更好的帮助将不胜感激?

c#

0
推荐指数
1
解决办法
1308
查看次数

标签 统计

c# ×1