-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] MVVM: (+WPF) 1

, 22 2017 . 18:34 +
oggr 18:34

MVVM: (+WPF) 1

MVVM WPF. . , , , WPF , , Prism.

MVVM? ! .

: . , , , . :


1: MVVM.


2: MVVM.

, , ?, . .

1:


:

ModelFirst.

  • 1. .
  • 2. .
  • 3. VM.

, . : . , .

, . - . - ( , ) , , , . , , . , , .

, . , , . .. . :

 static class MathFuncs {
    public static int GetSumOf(int a, int b) => a + b;
  }

(. ModelFirst) View , . , . , , . . . View , : , , .

View VM. VM , . .. View . View? , View , , / VM ( ) , /.

View , - . VM , ! VM View ( , ) , . , :


3: 1

View, . VM, (), , .

1 WPF


WPF MVVM. View XAML. .. (View) XAML. . , Binding. View VM , VM DataContext View.

View:

  

    


  
  
  
  
  
  

VM. VM View, INotifyPropertyChange. View , VM - .

:

public class MainVM : INotifyPropertyChange
{
  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged(string propertyName) {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }
}

VM . ( VM View , )

private int _number1;
public int Number1 { get {return _number1;}
  set { _number1 = value;
    OnPropertyChanged("Number3"); //  View  ,   
  }
}

private int _number2;
public int Number2 { get {return _number2;}
  set { _number1 = value; OnPropertyChanged("Number3"); } }

VM :

//   ,   View  ,   Number1  Number2
public int Number3 { get; } => MathFuncs.GetSumOf(Number1, Number2);

MVVM.

2:

. . ListBox . , . , ListBox' . .


4: 2

. stateless . . . . VM , . . , , .. . . . : INotifyPropertyChanged. , .

:

. , , , - . , , : ) , ) , , .

, Prism DelegateCommand, BindableBase INotifyPropertyChange. NuGet Prism.Wpf ( 6.3.0). OnPropertyChanged() RaisePropertyChanged().

public class MyMathModel : BindableBase
{
  private readonly ObservableCollection _myValues = new ObservableCollection();
  public readonly ReadOnlyObservableCollection MyPublicValues;
  public MyMathModel() {
    MyPublicValues = new ReadOnlyObservableCollection(_myValues);
  }
  //        
  public void AddValue(int value) {
    _myValues.Add(value);
    RaisePropertyChanged("Sum");
  }
  //  ,        
  public void RemoveValue(int index) {
      //      -  
    if (index >= 0 && index < _myValues.Count) _myValues.RemoveAt(index);
    RaisePropertyChanged("Sum");
  }
  public int Sum => MyPublicValues.Sum(); //
}

View. . , VM, DelegateCommand. , MVVM . . , .. Command.

, , , DelegateCommand, VM, DelegateCommand, VM . , , , .


5: 2

View View <=> ViewModel, View <=> View. , , , "{Binding ElementName=TheNumber, Path=Text}".


    
           
    
    
        
        
            
            
        
        
        
        
        
        
        
    

ViewModel:

public class MainVM : BindableBase
{
  readonly MyMathModel _model = new MyMathModel();
  public MainVM()
  {
    //         View
    _model.PropertyChanged += (s, e) => { RaisePropertyChanged(e.PropertyName); };
    AddCommand = new DelegateCommand(str => {
      //    -  VM
      int ival;
      if (int.TryParse(str, out ival)) _model.AddValue(ival);
    });
    RemoveCommand = new DelegateCommand(i => {
        if(i.HasValue) _model.RemoveValue(i.Value);
    });
  }
  public DelegateCommand AddCommand { get; }
  public DelegateCommand RemoveCommand { get; }
  public int Sum => _model.Sum;
  public ReadOnlyObservableCollection MyValues => _model.MyPublicValues;
}

! . VM , .. , , . VM . .. - ( ), View.

MVVM, ObservableCollection, DelegateCommand, View <=> View View.
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/338518/

:  

: [1] []
 

:
: 

: ( )

:

  URL