为什么不能从静态上下文中引用方法定义?

时间:2017-09-01 06:17:36

标签: c# wpf

无法从静态上下文访问非静态方法AddNew。

参考类:

public class MainActivity extends AppCompatActivity{

    BroadcastReceiver mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(getBaseContext(),TestService.class));
    }

}

public class TestService extends Service {

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("service", " destroyed");
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service started", Toast.LENGTH_SHORT).show();
        Log.e("service", " onStartCommand");
        return START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

我的代码:

public class RelayCommand : ICommand
{
    public RelayCommand(Action<object> execute);
    public RelayCommand(Action execute);
    public RelayCommand(Action execute, Func<bool> canExecute);
    public RelayCommand(Action<object> execute, Predicate<object> canExecute);
    public bool CanExecute(object parameter);
    public void Execute(object parameter);
    public event EventHandler CanExecuteChanged;
}

  [TypeForwardedFrom("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
  [ValueSerializer("System.Windows.Input.CommandValueSerializer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
  public interface ICommand
  {
    /// <summary>Defines the method that determines whether the command can execute in its current state.</summary>
    /// <returns>true if this command can be executed; otherwise, false.</returns>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    bool CanExecute(object parameter);
    /// <summary>Defines the method to be called when the command is invoked.</summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    void Execute(object parameter);
    /// <summary>Occurs when changes occur that affect whether or not the command should execute.</summary>
    event EventHandler CanExecuteChanged;
  }

来自Java,我希望这可以工作,因为已知这些方法在编译时静态存在?

1 个答案:

答案 0 :(得分:3)

只需在ExampleViewModel构造函数中初始化DelegateCommand:

public ExampleViewModel()
{
    AddNewCommand = new DelegateCommand(AddNew, CanAdd);
}

public ICommand AddNewCommand { get; private set; }

作为旁注,NewName永远不会评估为null,因为您给它的值为string.Empty,因此空合并运算符不会在那里做任何事情。< / p>