Binding Commands

This commit is contained in:
MaddoScientisto 2024-10-14 23:25:35 +02:00
commit 22f7143d6e
4 changed files with 156 additions and 16 deletions

View file

@ -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<Task> _execute;
private readonly Func<bool> _canExecute;
private bool _isExecuting;
public event EventHandler CanExecuteChanged;
public AsyncCommand(Func<Task> execute, Func<bool> 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);
}
}
}

View file

@ -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<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> 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);
}
}

View file

@ -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");
}
}
}

View file

@ -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
{