From 22f7143d6e3c3ad93ed30ab1abe82e1ee650243f Mon Sep 17 00:00:00 2001 From: MaddoScientisto Date: Mon, 14 Oct 2024 23:25:35 +0200 Subject: [PATCH] Binding Commands --- imagecatalog/Commands/AsyncCommand.cs | 52 +++++++++++++++++++++++++ imagecatalog/Commands/RelayCommand.cs | 41 ++++++++++++++++++++ imagecatalog/DataModel.cs | 23 ++++++++++- imagecatalog/MainForm.Designer.cs | 56 ++++++++++++++++++++------- 4 files changed, 156 insertions(+), 16 deletions(-) create mode 100644 imagecatalog/Commands/AsyncCommand.cs create mode 100644 imagecatalog/Commands/RelayCommand.cs diff --git a/imagecatalog/Commands/AsyncCommand.cs b/imagecatalog/Commands/AsyncCommand.cs new file mode 100644 index 0000000..62334d0 --- /dev/null +++ b/imagecatalog/Commands/AsyncCommand.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace ImageCatalog_2.Commands +{ + public class AsyncCommand : ICommand + { + private readonly Func _execute; + private readonly Func _canExecute; + private bool _isExecuting; + + public event EventHandler CanExecuteChanged; + + public AsyncCommand(Func execute, Func canExecute = null) + { + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + _canExecute = canExecute; + } + + public bool CanExecute(object parameter) + { + return (_canExecute?.Invoke() ?? true) && !_isExecuting; + } + + public async void Execute(object parameter) + { + if (CanExecute(parameter)) + { + try + { + _isExecuting = true; + RaiseCanExecuteChanged(); + await _execute(); + } + finally + { + _isExecuting = false; + RaiseCanExecuteChanged(); + } + } + } + + public void RaiseCanExecuteChanged() + { + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + } + } +} diff --git a/imagecatalog/Commands/RelayCommand.cs b/imagecatalog/Commands/RelayCommand.cs new file mode 100644 index 0000000..81e92e4 --- /dev/null +++ b/imagecatalog/Commands/RelayCommand.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace ImageCatalog_2.Commands; + +public class RelayCommand : System.Windows.Input.ICommand +{ + private readonly Action _execute; + private readonly Predicate _canExecute; + + public RelayCommand(Action execute) : this(execute, null) { } + + public RelayCommand(Action execute, Predicate canExecute) + { + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + _canExecute = canExecute; + } + + // Manually raise this event in WinForms as CommandManager is unavailable + public event EventHandler CanExecuteChanged; + + public bool CanExecute(object parameter) + { + return _canExecute == null || _canExecute(parameter); + } + + public void Execute(object parameter) + { + _execute(parameter); + } + + // Method to manually raise CanExecuteChanged event + public void RaiseCanExecuteChanged() + { + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + } +} \ No newline at end of file diff --git a/imagecatalog/DataModel.cs b/imagecatalog/DataModel.cs index 1a6c349..700e05f 100644 --- a/imagecatalog/DataModel.cs +++ b/imagecatalog/DataModel.cs @@ -1,19 +1,30 @@ -using ImageCatalog_2.Services; +using ImageCatalog_2.Commands; +using ImageCatalog_2.Services; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Input; namespace ImageCatalog_2 { public class DataModel : ViewModelBase { + + public ICommand TestCommand { get; } + + public ICommand AsyncTestCommand { get; } + private readonly ITestService _service; public DataModel(ITestService testService) { _service = testService; + + TestCommand = new RelayCommand(Test); + AsyncTestCommand = new AsyncCommand(TestAsync); } private string _sourcePath; @@ -39,5 +50,15 @@ namespace ImageCatalog_2 } } + private void Test(object parameter) + { + Debug.WriteLine("Yep"); + } + + private async Task TestAsync() + { + Debug.WriteLine("Yep c"); + } + } } diff --git a/imagecatalog/MainForm.Designer.cs b/imagecatalog/MainForm.Designer.cs index a98ad26..8b069f7 100644 --- a/imagecatalog/MainForm.Designer.cs +++ b/imagecatalog/MainForm.Designer.cs @@ -43,6 +43,10 @@ namespace ImageCatalog Label43 = new Label(); TabControl1 = new TabControl(); TabPage5 = new TabPage(); + button1 = new Button(); + btnTest = new Button(); + bindingSource1 = new BindingSource(components); + dataModelBindingSource = new BindingSource(components); GroupBox11 = new GroupBox(); Panel3 = new Panel(); rdbNuovoMetodo = new RadioButton(); @@ -58,8 +62,6 @@ namespace ImageCatalog Label1 = new Label(); Label2 = new Label(); txtSorgente = new TextBox(); - bindingSource1 = new BindingSource(components); - dataModelBindingSource = new BindingSource(components); txtDestinazione = new TextBox(); GroupBox8 = new GroupBox(); rdbNumFiles = new RadioButton(); @@ -181,11 +183,11 @@ namespace ImageCatalog dataModelBindingSource1 = new BindingSource(components); TabControl1.SuspendLayout(); TabPage5.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).BeginInit(); GroupBox11.SuspendLayout(); Panel3.SuspendLayout(); GroupBox3.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit(); - ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).BeginInit(); GroupBox8.SuspendLayout(); GroupBox7.SuspendLayout(); TabPage3.SuspendLayout(); @@ -253,6 +255,8 @@ namespace ImageCatalog // // TabPage5 // + TabPage5.Controls.Add(button1); + TabPage5.Controls.Add(btnTest); TabPage5.Controls.Add(GroupBox11); TabPage5.Controls.Add(GroupBox3); TabPage5.Controls.Add(GroupBox8); @@ -266,6 +270,34 @@ namespace ImageCatalog TabPage5.Text = "Generale"; TabPage5.UseVisualStyleBackColor = true; // + // button1 + // + button1.DataBindings.Add(new Binding("Command", bindingSource1, "AsyncTestCommand", true)); + button1.Location = new Point(501, 480); + button1.Name = "button1"; + button1.Size = new Size(94, 29); + button1.TabIndex = 50; + button1.Text = "Test Async"; + button1.UseVisualStyleBackColor = true; + // + // btnTest + // + btnTest.DataBindings.Add(new Binding("Command", bindingSource1, "TestCommand", true)); + btnTest.Location = new Point(325, 472); + btnTest.Name = "btnTest"; + btnTest.Size = new Size(94, 29); + btnTest.TabIndex = 49; + btnTest.Text = "Test"; + btnTest.UseVisualStyleBackColor = true; + // + // bindingSource1 + // + bindingSource1.DataSource = dataModelBindingSource; + // + // dataModelBindingSource + // + dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel); + // // GroupBox11 // GroupBox11.Controls.Add(Panel3); @@ -379,7 +411,7 @@ namespace ImageCatalog chkAggiornaSottodirectory.Location = new Point(107, 98); chkAggiornaSottodirectory.Margin = new Padding(4, 5, 4, 5); chkAggiornaSottodirectory.Name = "chkAggiornaSottodirectory"; - chkAggiornaSottodirectory.Size = new Size(203, 37); + chkAggiornaSottodirectory.Size = new Size(317, 37); chkAggiornaSottodirectory.TabIndex = 25; chkAggiornaSottodirectory.Text = "aggiorna le sottodirectory"; // @@ -435,14 +467,6 @@ namespace ImageCatalog txtSorgente.TabIndex = 0; txtSorgente.Text = "TextBox1"; // - // bindingSource1 - // - bindingSource1.DataSource = dataModelBindingSource; - // - // dataModelBindingSource - // - dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel); - // // txtDestinazione // txtDestinazione.DataBindings.Add(new Binding("Text", bindingSource1, "DestinationPath", true, DataSourceUpdateMode.OnPropertyChanged)); @@ -1795,14 +1819,14 @@ namespace ImageCatalog Load += Form1_Load; TabControl1.ResumeLayout(false); TabPage5.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit(); + ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).EndInit(); GroupBox11.ResumeLayout(false); GroupBox11.PerformLayout(); Panel3.ResumeLayout(false); Panel3.PerformLayout(); GroupBox3.ResumeLayout(false); GroupBox3.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit(); - ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).EndInit(); GroupBox8.ResumeLayout(false); GroupBox8.PerformLayout(); GroupBox7.ResumeLayout(false); @@ -2305,6 +2329,8 @@ namespace ImageCatalog private BindingSource dataModelBindingSource; private BindingSource dataModelBindingSource1; private BindingSource bindingSource1; + private Button btnTest; + private Button button1; internal Button btnCreaCatalogoAsync {