2017-09-01 6 views
0

静的コンテキストから非静的メソッドAddNewにアクセスできません。静的コンテキストからメソッド定義を参照できないのはなぜですか?

参照されるクラス:

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から来る
public class ExampleViewModel: NotificationObject 
{ 

    public ICommand AddNewCommand { get; } = new Microsoft.Practices.Prism.Commands.DelegateCommand(AddNew, CanAdd); // method references arn't static?? 

    public String NewName { get; set; } = ""; 

    internal bool CanAdd() 
    { 
     //Can Add if string is non empty & non null 
     return !string.IsNullOrEmpty(NewName); 
    } 

    internal void AddNew() 
    { 
     var name = NewName ?? "NeedToCheckforNull"; 

     var newWindSpeedEnv = new WindSpeedEnvelope() 
     { 
      Id = Guid.NewGuid(), 
      Name = name 
     }; 
    } 
} 

方法は、コンパイル時に静的に存在することが知られているように、私は、仕事にこれを期待しているのでしょうか?

答えて

1

だけExampleViewModelコンストラクタでDelegateCommandを初期化:サイドノートとして

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

public ICommand AddNewCommand { get; private set; } 

NewNameがnullに評価することはありません、あなたはそれをstring.Emptyの値を与えているとして、null合体演算子がないので、そこに何かする。

+0

このセクションのコードは、私が古いクラスからコピーして、物を掃除しようとしている場所をコピーしているので、まだまだフラックスになっています。 私はそれが正しく動作するはずです自動プロパティのイニシャライザとしてではなく、コンストラクタの内部に設定する場合、私は理解していますか? –

+0

する必要があります、私は自分自身を確認するが、Prismのダウンロードサイズは恐ろしいです:-) –

+0

私が参照したクラスの最小限の定義を含む答えを編集しました。 –

関連する問題