2017-09-17 37 views
-2

我在MVVM一個新手,我有一個Model.cs其中包含的一些屬性‘ID’和‘PositionName’我得到了View.cs這包含點擊我在「Items.PositionName == NULL」遇到NullReference錯誤後SelectedItems = {綁定表項}和的ItemSource = {裝訂位置}的DataGrid,並用命令= {結合SHowEdits}按鈕。
這是我的代碼。C# - System.NullReferenceException「類型的未處理的異常'

ViewModel.cs

class PositionVM : INotifyPropertyChanged 
{ 
    private ObservableCollection<PositionModel> _position; 
    private PositionModel _items; 
    private ICommand _showedits; 
    public ObservableCollection<PositionModel> Position 
    { 
     get 
     { 
      return _position; 
     } 
     set 
     { 
      _position = value; 
      NotifyProperty("Position"); 
     } 
    } 
    public PositionModel Items 
    { 
     get 
     { 
      return _items; 
     } 
     set 
     { 
      _items = value; 
      NotifyProperty("Items"); 
     } 
    } 
    public ICommand ShowEdits 
    { 
     get 
     { 
      if (_showedits == null) 
       _showedits = new ShowEdit(); 
      return _showedits; 
     } 
     set 
     { 
      _showedits = value; 
     } 
    } 
    public PositionVM() 
    { 
     Position = new ObservableCollection<PositionModel>(); 
     Position.Add(new PositionModel() 
     { 
      ID = 1, 
      PositionName = "asd" 
     }); 
    } 
    public void ShowEditDialog() 
    { 
     if (Items.PositionName == null) 
     { 
      MessageBox.Show("ERROR"); 
     } 
     else 
     { 
      PositionView view = new PositionView(); 
      Data.ID = view.txtid.Text; 
      var z = new PositionView(); 
      z.ShowDialog(); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyProperty(String info) 
    { 
     if(PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

爲什麼會出現這個錯誤?我如何避免它? Thanksss

回答

0

我想用的ICommand您的問題。嘗試做它的財產:

public ICommand ShowEditsCommand { get; set; }; 

當然,你需要創建它。我用我的命令類,你可以用它太在第一次,這是很簡單的:

/// <summary> 
/// Relay implementation of ICommand. 
/// </summary> 
public class RelayCommand : ICommand 
{ 
    private Action execute; 

    private Predicate<object> canExecute; 

    private event EventHandler CanExecuteChangedInternal; 

    public RelayCommand(Action execute) 
     : this(execute, DefaultCanExecute) 
    { 
    } 

    public RelayCommand(Action execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
     { 
      throw new ArgumentNullException("execute"); 
     } 

     if (canExecute == null) 
     { 
      throw new ArgumentNullException("canExecute"); 
     } 

     this.execute = execute; 
     this.canExecute = canExecute; 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add 
     { 
      CommandManager.RequerySuggested += value; 
      this.CanExecuteChangedInternal += value; 
     } 

     remove 
     { 
      CommandManager.RequerySuggested -= value; 
      this.CanExecuteChangedInternal -= value; 
     } 
    } 

    public bool CanExecute(object parameter) 
    { 
     return this.canExecute != null && this.canExecute(parameter); 
    } 

    public void Execute(object parameter) 
    { 
     this.execute(); 
    } 

    public void OnCanExecuteChanged() 
    { 
     EventHandler handler = this.CanExecuteChangedInternal; 
     if (handler != null) 
     { 
      handler.Invoke(this, EventArgs.Empty); 
     } 
    } 

    public void Destroy() 
    { 
     this.canExecute = _ => false; 
     this.execute =() => { return; }; 
    } 

    private static bool DefaultCanExecute(object parameter) 
    { 
     return true; 
    } 
} 

這一點,你需要初始化它在你的視圖模型構造並綁定到方法後:

ShowEditsCommand = new RelayCommand(ShowEdits); 

其中ShowEdits是您需要在命令調用時運行的方法:

public void ShowEdits() 
{ 
// do something here 
} 
0

由於該行

Items.PositionName == null 

拋出一個空引用異常,很明顯,Items爲空。

爲什麼Items爲空?

Items屬性的定義如下:當您嘗試設置吸氣劑(get { return _items; })呼叫完成的PositionModel

public PositionModel Items 
{ 
    get 
    { 
     return _items; 
    } 
    set 
    { 
     _items = value; 
     NotifyProperty("Items"); 
    } 
} 

,爲了你的對象的引用_items指向。您獲得的值爲null。這意味着,無論是二傳沒有被調用,以_items用值來初始化或已被初始化,但後來你的代碼的另一部分已經將其設置爲null。通過您的代碼

來看,我們看到你的類的構造函數:

public PositionVM() 
{ 
    Position = new ObservableCollection<PositionModel>(); 
    Position.Add(new PositionModel() 
    { 
     ID = 1, 
     PositionName = "asd" 
    }); 
} 

顯然,Items不被初始化那裏。所以Items有引用類型,空默認值...

相關問題