mirror of
https://gitee.com/csharpui/CPF.git
synced 2025-06-28 13:34:09 +08:00
1
This commit is contained in:
parent
40efe5292f
commit
8decf9e554
@ -6,6 +6,7 @@ using CPF.Drawing;
|
|||||||
using CPF.Shapes;
|
using CPF.Shapes;
|
||||||
using CPF.Styling;
|
using CPF.Styling;
|
||||||
using CPF.Svg;
|
using CPF.Svg;
|
||||||
|
using CPF.Toolkit.Controls;
|
||||||
using CPF.Toolkit.Dialogs;
|
using CPF.Toolkit.Dialogs;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -70,6 +71,11 @@ namespace CPF.Toolkit.Demo
|
|||||||
Content = "loading",
|
Content = "loading",
|
||||||
Commands = { { nameof(Button.Click),(s,e) => vm.LoadingTest() } }
|
Commands = { { nameof(Button.Click),(s,e) => vm.LoadingTest() } }
|
||||||
},
|
},
|
||||||
|
new AsyncButton
|
||||||
|
{
|
||||||
|
Content = "AsyncButton",
|
||||||
|
Command = vm.AsyncClick,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using CPF.Controls;
|
using CPF.Controls;
|
||||||
|
using CPF.Toolkit.Input;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@ -47,5 +48,11 @@ namespace CPF.Toolkit.Demo
|
|||||||
//});
|
//});
|
||||||
//this.Dialog.Sucess(result);
|
//this.Dialog.Sucess(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IAsyncCommand AsyncClick => new AsyncCommand(async () =>
|
||||||
|
{
|
||||||
|
await Task.Delay(5000);
|
||||||
|
this.Dialog.Alert("test");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,5 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\CPF\CPF.csproj" />
|
<ProjectReference Include="..\CPF\CPF.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
</Project>
|
||||||
|
31
CPF.Toolkit/Controls/AsyncButton.cs
Normal file
31
CPF.Toolkit/Controls/AsyncButton.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
CPF.Toolkit/Input/AsyncCommand.cs
Normal file
51
CPF.Toolkit/Input/AsyncCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
CPF.Toolkit/Input/IAsyncCommand.cs
Normal file
14
CPF.Toolkit/Input/IAsyncCommand.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -65,6 +65,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPF.Toolkit", "CPF.Toolkit\
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPF.Toolkit.Demo", "CPF.Toolkit.Demo\CPF.Toolkit.Demo.csproj", "{AEA7FF33-1621-4A0A-9C08-6C4CB10C76FF}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPF.Toolkit.Demo", "CPF.Toolkit.Demo\CPF.Toolkit.Demo.csproj", "{AEA7FF33-1621-4A0A-9C08-6C4CB10C76FF}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{C2358108-0235-4151-97F6-0283CFF98093}"
|
||||||
|
ProjectSection(SolutionItems) = preProject
|
||||||
|
Directory.Build.props = Directory.Build.props
|
||||||
|
EndProjectSection
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
6
Directory.Build.props
Normal file
6
Directory.Build.props
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project>
|
||||||
|
<PropertyGroup>
|
||||||
|
<LangVersion>preview</LangVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user