This commit is contained in:
luxiaoqi 2023-11-23 13:38:29 +08:00
parent 40efe5292f
commit 8decf9e554
9 changed files with 123 additions and 5 deletions

View File

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

View File

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

View File

@ -21,7 +21,5 @@
<ItemGroup>
<ProjectReference Include="..\CPF\CPF.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>
</Project>

View File

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

View File

@ -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<Task> execute)
{
this.execute = execute;
}
private readonly Func<Task> 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;
}
}
}

View File

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

View File

@ -260,7 +260,7 @@ namespace CPF.Controls
OnClick(new RoutedEventArgs(this));
//}
}
protected virtual void OnClick(RoutedEventArgs e)
{
RaiseEvent(e, nameof(Click));

View File

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

6
Directory.Build.props Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>