diff --git a/CPF.Toolkit.Demo/MainView.cs b/CPF.Toolkit.Demo/MainView.cs index 84817ed..52887b2 100644 --- a/CPF.Toolkit.Demo/MainView.cs +++ b/CPF.Toolkit.Demo/MainView.cs @@ -6,6 +6,7 @@ using CPF.Drawing; using CPF.Shapes; using CPF.Styling; using CPF.Svg; +using CPF.Toolkit.Controls; using CPF.Toolkit.Dialogs; using System; using System.Collections.Generic; @@ -70,6 +71,11 @@ namespace CPF.Toolkit.Demo Content = "loading", Commands = { { nameof(Button.Click),(s,e) => vm.LoadingTest() } } }, + new AsyncButton + { + Content = "AsyncButton", + Command = vm.AsyncClick, + }, } })); } diff --git a/CPF.Toolkit.Demo/MainViewModel.cs b/CPF.Toolkit.Demo/MainViewModel.cs index f38b421..c3c4dd9 100644 --- a/CPF.Toolkit.Demo/MainViewModel.cs +++ b/CPF.Toolkit.Demo/MainViewModel.cs @@ -1,4 +1,5 @@ using CPF.Controls; +using CPF.Toolkit.Input; using System; using System.Collections.Generic; using System.Diagnostics; @@ -28,7 +29,7 @@ namespace CPF.Toolkit.Demo public async void LoadingTest() { - await this.ShowLoading(async () => + await this.ShowLoading(async () => { await Task.Delay(1000); Debug.WriteLine(1); @@ -47,5 +48,11 @@ namespace CPF.Toolkit.Demo //}); //this.Dialog.Sucess(result); } + + public IAsyncCommand AsyncClick => new AsyncCommand(async () => + { + await Task.Delay(5000); + this.Dialog.Alert("test"); + }); } } diff --git a/CPF.Toolkit/CPF.Toolkit.csproj b/CPF.Toolkit/CPF.Toolkit.csproj index 4e089e0..a49aac9 100644 --- a/CPF.Toolkit/CPF.Toolkit.csproj +++ b/CPF.Toolkit/CPF.Toolkit.csproj @@ -21,7 +21,5 @@ - - - + diff --git a/CPF.Toolkit/Controls/AsyncButton.cs b/CPF.Toolkit/Controls/AsyncButton.cs new file mode 100644 index 0000000..f7aff72 --- /dev/null +++ b/CPF.Toolkit/Controls/AsyncButton.cs @@ -0,0 +1,31 @@ +using CPF.Controls; +using CPF.Toolkit.Input; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CPF.Toolkit.Controls +{ + public class AsyncButton : Button + { + + protected override void InitializeComponent() + { + base.InitializeComponent(); + this.Triggers.Add(nameof(IsEnabled), Relation.Me, null, (nameof(Background), "224,224,224")); + } + + public IAsyncCommand Command { get; set; } + + protected override async void OnClick(RoutedEventArgs e) + { + this.IsEnabled = false; + base.OnClick(e); + if (this.Command != null) + { + await this.Command.ExecuteAsync(); + } + this.IsEnabled = true; + } + } +} diff --git a/CPF.Toolkit/Input/AsyncCommand.cs b/CPF.Toolkit/Input/AsyncCommand.cs new file mode 100644 index 0000000..c70a06a --- /dev/null +++ b/CPF.Toolkit/Input/AsyncCommand.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace CPF.Toolkit.Input +{ + public class AsyncCommand : IAsyncCommand + { + public AsyncCommand(Func execute) + { + this.execute = execute; + } + + private readonly Func execute; + private Task executionTask; + + public Task ExecutionTask + { + get => this.executionTask; + private set + { + if (ReferenceEquals(this.executionTask, value)) + { + return; + } + + this.executionTask = value; + bool isAlreadyCompletedOrNull = value?.IsCompleted ?? true; + if (isAlreadyCompletedOrNull) + { + return; + } + } + } + + public bool IsRunning => ExecutionTask is { IsCompleted: false }; + + + public Task ExecuteAsync() + { + Task executionTask = ExecutionTask; + if (this.execute is not null) + { + executionTask = ExecutionTask = this.execute(); + } + return executionTask; + } + } +} diff --git a/CPF.Toolkit/Input/IAsyncCommand.cs b/CPF.Toolkit/Input/IAsyncCommand.cs new file mode 100644 index 0000000..e2c2f45 --- /dev/null +++ b/CPF.Toolkit/Input/IAsyncCommand.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace CPF.Toolkit.Input +{ + public interface IAsyncCommand + { + Task ExecutionTask { get; } + bool IsRunning { get; } + Task ExecuteAsync(); + } +} diff --git a/CPF/Controls/ButtonBase.cs b/CPF/Controls/ButtonBase.cs index 787b136..142f299 100644 --- a/CPF/Controls/ButtonBase.cs +++ b/CPF/Controls/ButtonBase.cs @@ -260,7 +260,7 @@ namespace CPF.Controls OnClick(new RoutedEventArgs(this)); //} } - + protected virtual void OnClick(RoutedEventArgs e) { RaiseEvent(e, nameof(Click)); diff --git a/ConsoleApp1.sln b/ConsoleApp1.sln index 4269b1e..13cf77a 100644 --- a/ConsoleApp1.sln +++ b/ConsoleApp1.sln @@ -65,6 +65,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPF.Toolkit", "CPF.Toolkit\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPF.Toolkit.Demo", "CPF.Toolkit.Demo\CPF.Toolkit.Demo.csproj", "{AEA7FF33-1621-4A0A-9C08-6C4CB10C76FF}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{C2358108-0235-4151-97F6-0283CFF98093}" + ProjectSection(SolutionItems) = preProject + Directory.Build.props = Directory.Build.props + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..66fcb36 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,6 @@ + + + + preview + + \ No newline at end of file