سلام
وقت بخیر
ما اگه بخایم یه command رو پیاده سازی کنیم، اینطوری عمل میکنیم:
- public class NormalCommand : ICommand
- {
- public event EventHandler CanExecuteChanged;
- public bool CanExecute(object parameter)
- {
- throw new NotImplementedException();
- }
- public void Execute(object parameter)
- {
- throw new NotImplementedException();
- }
- }
ولی برای الگوی mvvm، ما نمیتونیم که برای هر متدی، یک کلاس اینطوری بنویسیم.
لذا یک کلاس relaycommand میسازیم و همیشه ازش استفاده میکنیم.
- public class RelayCommand : ICommand
- {
- private Action<object> execute;
- private Func<object, bool> canExecute;
- public event EventHandler CanExecuteChanged
- {
- add { CommandManager.RequerySuggested += value; }
- remove { CommandManager.RequerySuggested -= value; }
- }
- public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
- {
- this.execute = execute;
- this.canExecute = canExecute;
- }
- public bool CanExecute(object parameter)
- {
- retu this.canExecute == null || this.canExecute(parameter);
- }
- public void Execute(object parameter)
- {
- this.execute(parameter);
- }
- }
ولی سوالی که برای من پیش اومده و هنوز نتونستم جوابم رو پیدا کنم ، این هست که چرا رویداد command رو در relaycommand اینطوری مینویسه؟
- public event EventHandler CanExecuteChanged
- {
- add { CommandManager.RequerySuggested += value; }
- remove { CommandManager.RequerySuggested -= value; }
- }
راستش با add و remove آشنایی ندارم.
همچنین با کدهایی که در این دو بلاک نوشته.
پیشاپیش ممنون بابت گذاشن وقت
- public event EventHandler CanExecuteChanged
- {
- add { CommandManager.RequerySuggested += value; }
- remove { CommandManager.RequerySuggested -= value; }
- }
برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید
برچسب : نویسنده : محمد رضا جوادیان programers بازدید : 83 تاريخ : سه شنبه 11 خرداد 1395 ساعت: 13:54