mirror of
https://github.com/LorisYounger/VPet.ModMaker.git
synced 2024-08-30 18:22:21 +00:00
更新 SimpleObservable
This commit is contained in:
parent
41fd4bf738
commit
4f1c92c82b
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -12,26 +13,8 @@ namespace HKW.HKWViewModels.SimpleObservable;
|
||||
/// </summary>
|
||||
public class ObservableCommand : ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行的方法
|
||||
/// </summary>
|
||||
public Action? ExecuteAction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行的异步方法
|
||||
/// </summary>
|
||||
public Func<Task>? ExecuteActionAsync { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取能否执行的方法
|
||||
/// </summary>
|
||||
public Func<bool>? CanExecuteAction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 能执行的属性
|
||||
/// <para>
|
||||
/// 注意: 仅当 <see cref="CanExecuteAction"/> 为 <see langword="null"/> 时, 此属性才会被使用
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public ObservableValue<bool> CanExecuteProperty { get; } = new(true);
|
||||
|
||||
@ -64,9 +47,7 @@ public class ObservableCommand : ICommand
|
||||
{
|
||||
if (r_waiting.Value is true)
|
||||
return false;
|
||||
return CanExecuteAction is null
|
||||
? CanExecuteProperty.Value
|
||||
: CanExecuteAction?.Invoke() is not false;
|
||||
return CanExecuteProperty.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -75,7 +56,7 @@ public class ObservableCommand : ICommand
|
||||
/// <param name="parameter">参数</param>
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
ExecuteAction?.Invoke();
|
||||
ExecuteEvent?.Invoke();
|
||||
await ExecuteAsync();
|
||||
}
|
||||
|
||||
@ -85,15 +66,39 @@ public class ObservableCommand : ICommand
|
||||
/// <returns>等待</returns>
|
||||
private async Task ExecuteAsync()
|
||||
{
|
||||
if (ExecuteActionAsync is null)
|
||||
if (AsyncExecuteEvent is null)
|
||||
return;
|
||||
r_waiting.Value = true;
|
||||
await ExecuteActionAsync.Invoke();
|
||||
foreach (
|
||||
var asyncEvent in AsyncExecuteEvent.GetInvocationList().Cast<AsyncExecuteHandler>()
|
||||
)
|
||||
await asyncEvent.Invoke();
|
||||
r_waiting.Value = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 能否执行属性被改变事件
|
||||
/// 能否执行属性改变后事件
|
||||
/// </summary>
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 执行事件
|
||||
/// </summary>
|
||||
public event ExecuteHandler? ExecuteEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 异步执行事件
|
||||
/// </summary>
|
||||
public event AsyncExecuteHandler? AsyncExecuteEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 执行
|
||||
/// </summary>
|
||||
public delegate void ExecuteHandler();
|
||||
|
||||
/// <summary>
|
||||
/// 异步执行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public delegate Task AsyncExecuteHandler();
|
||||
}
|
||||
|
@ -14,22 +14,13 @@ namespace HKW.HKWViewModels.SimpleObservable;
|
||||
public class ObservableCommand<T> : ICommand
|
||||
where T : notnull
|
||||
{
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteAction"/>
|
||||
public Action<T?>? ExecuteAction { get; set; }
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteActionAsync"/>
|
||||
public Func<T?, Task>? ExecuteActionAsync { get; set; }
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.CanExecuteAction"/>
|
||||
public Func<T?, bool>? CanExecuteAction { get; set; }
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.CanExecuteProperty"/>
|
||||
public ObservableValue<bool> CanExecuteProperty { get; } = new(true);
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.r_waiting"/>
|
||||
private readonly ObservableValue<bool> r_waiting = new(false);
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.ObservableCommand()"/>
|
||||
/// <inheritdoc />
|
||||
public ObservableCommand()
|
||||
{
|
||||
CanExecuteProperty.PropertyChanged += InvokeCanExecuteChanged;
|
||||
@ -49,29 +40,44 @@ public class ObservableCommand<T> : ICommand
|
||||
{
|
||||
if (r_waiting.Value is true)
|
||||
return false;
|
||||
return CanExecuteAction is null
|
||||
? CanExecuteProperty.Value
|
||||
: CanExecuteAction?.Invoke((T?)parameter) is not false;
|
||||
return CanExecuteProperty.Value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.Execute(object?)"/>
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
ExecuteAction?.Invoke((T?)parameter);
|
||||
await ExecuteAsync((T?)parameter);
|
||||
ExecuteEvent?.Invoke((T?)parameter!);
|
||||
await ExecuteAsync((T?)parameter!);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteActionAsync()"/>
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteAsync"/>
|
||||
/// <param name="parameter">参数</param>
|
||||
private async Task ExecuteAsync(T? parameter)
|
||||
private async Task ExecuteAsync(T parameter)
|
||||
{
|
||||
if (ExecuteActionAsync is null)
|
||||
if (AsyncExecuteEvent is null)
|
||||
return;
|
||||
r_waiting.Value = true;
|
||||
await ExecuteActionAsync.Invoke(parameter);
|
||||
foreach (
|
||||
var asyncEvent in AsyncExecuteEvent.GetInvocationList().Cast<AsyncExecuteHandler>()
|
||||
)
|
||||
await asyncEvent.Invoke(parameter);
|
||||
r_waiting.Value = false;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.CanExecuteChanged"/>
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteEvent"/>
|
||||
public event ExecuteHandler? ExecuteEvent;
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.AsyncExecuteEvent"/>
|
||||
public event AsyncExecuteHandler? AsyncExecuteEvent;
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.ExecuteHandler"/>
|
||||
/// <param name="value">值</param>
|
||||
public delegate void ExecuteHandler(T value);
|
||||
|
||||
/// <inheritdoc cref="ObservableCommand.AsyncExecuteHandler"/>
|
||||
/// <param name="value">值</param>
|
||||
public delegate Task AsyncExecuteHandler(T value);
|
||||
}
|
@ -102,6 +102,7 @@
|
||||
<Compile Include="Models\ObservableRange.cs" />
|
||||
<Compile Include="Models\PetModel.cs" />
|
||||
<Compile Include="Models\SelectTextModel.cs" />
|
||||
<Compile Include="SimpleObservable\ObservableCommandT.cs" />
|
||||
<Compile Include="ViewModels\ModEdit\ClickTextEdit\ClickTextEditWindowVM.cs" />
|
||||
<Compile Include="ViewModels\ModEdit\ClickTextEdit\ClickTextPageVM.cs" />
|
||||
<Compile Include="ViewModels\ModEdit\FoodEdit\FoodPageVM.cs" />
|
||||
@ -145,7 +146,6 @@
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="SimpleObservable\ObservableCommand.cs" />
|
||||
<Compile Include="SimpleObservable\ObservableCommand{T}.cs" />
|
||||
<Compile Include="SimpleObservable\ObservableValue.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
<Compile Include="Views\ModEdit\FoodEdit\FoodEditWindow.xaml.cs">
|
||||
|
@ -29,9 +29,9 @@ public class ClickTextPageVM
|
||||
{
|
||||
ShowClickTexts.Value = ClickTexts;
|
||||
FilterClickText.ValueChanged += FilterClickText_ValueChanged;
|
||||
AddClickTextCommand.ExecuteAction = AddClickText;
|
||||
EditClickTextCommand.ExecuteAction = EditClickText;
|
||||
RemoveClickTextCommand.ExecuteAction = RemoveClickText;
|
||||
AddClickTextCommand.ExecuteEvent += AddClickText;
|
||||
EditClickTextCommand.ExecuteEvent += EditClickText;
|
||||
RemoveClickTextCommand.ExecuteEvent += RemoveClickText;
|
||||
}
|
||||
|
||||
private void FilterClickText_ValueChanged(string value)
|
||||
|
@ -29,8 +29,8 @@ public class FoodEditWindowVM
|
||||
public FoodEditWindowVM()
|
||||
{
|
||||
InitializeFoodTypes();
|
||||
AddImageCommand.ExecuteAction = AddImage;
|
||||
ChangeImageCommand.ExecuteAction = ChangeImage;
|
||||
AddImageCommand.ExecuteEvent += AddImage;
|
||||
ChangeImageCommand.ExecuteEvent += ChangeImage;
|
||||
}
|
||||
|
||||
public void Close() { }
|
||||
|
@ -31,9 +31,9 @@ public class FoodPageVM
|
||||
ShowFoods.Value = Foods;
|
||||
FilterFoodText.ValueChanged += FilterFoodText_ValueChanged;
|
||||
|
||||
AddFoodCommand.ExecuteAction = AddFood;
|
||||
EditFoodCommand.ExecuteAction = EditFood;
|
||||
RemoveFoodCommand.ExecuteAction = RemoveFood;
|
||||
AddFoodCommand.ExecuteEvent += AddFood;
|
||||
EditFoodCommand.ExecuteEvent += EditFood;
|
||||
RemoveFoodCommand.ExecuteEvent += RemoveFood;
|
||||
}
|
||||
|
||||
private void FilterFoodText_ValueChanged(string value)
|
||||
|
@ -32,9 +32,9 @@ public class LowTextPageVM
|
||||
{
|
||||
ShowLowTexts.Value = LowTexts;
|
||||
FilterLowText.ValueChanged += FilterLowText_ValueChanged;
|
||||
AddLowTextCommand.ExecuteAction = AddLowText;
|
||||
EditLowTextCommand.ExecuteAction = EditLowText;
|
||||
RemoveLowTextCommand.ExecuteAction = RemoveLowText;
|
||||
AddLowTextCommand.ExecuteEvent += AddLowText;
|
||||
EditLowTextCommand.ExecuteEvent += EditLowText;
|
||||
RemoveLowTextCommand.ExecuteEvent += RemoveLowText;
|
||||
}
|
||||
|
||||
private void FilterLowText_ValueChanged(string value)
|
||||
|
@ -79,13 +79,13 @@ public class ModEditWindowVM
|
||||
ModEditWindow = window;
|
||||
CurrentLang.ValueChanged += CurrentLang_ValueChanged;
|
||||
|
||||
AddImageCommand.ExecuteAction += AddImage;
|
||||
ChangeImageCommand.ExecuteAction += ChangeImage;
|
||||
AddLangCommand.ExecuteAction += AddLang;
|
||||
EditLangCommand.ExecuteAction += EditLang;
|
||||
RemoveLangCommand.ExecuteAction += RemoveLang;
|
||||
SaveCommand.ExecuteAction += Save;
|
||||
SaveToCommand.ExecuteAction += SaveTo;
|
||||
AddImageCommand.ExecuteEvent += AddImage;
|
||||
ChangeImageCommand.ExecuteEvent += ChangeImage;
|
||||
AddLangCommand.ExecuteEvent += AddLang;
|
||||
EditLangCommand.ExecuteEvent += EditLang;
|
||||
RemoveLangCommand.ExecuteEvent += RemoveLang;
|
||||
SaveCommand.ExecuteEvent += Save;
|
||||
SaveToCommand.ExecuteEvent += SaveTo;
|
||||
}
|
||||
|
||||
private void CurrentLang_ValueChanged(string value)
|
||||
|
@ -42,9 +42,9 @@ public class ModMakerWindowVM
|
||||
LoadHistories();
|
||||
ModMakerWindow = window;
|
||||
ShowHistories.Value = Histories;
|
||||
CreateNewModCommand.ExecuteAction = CreateNewMod;
|
||||
LoadModFromFileCommand.ExecuteAction = LoadModFromFile;
|
||||
ClearHistoriesCommand.ExecuteAction = ClearHistories;
|
||||
CreateNewModCommand.ExecuteEvent += CreateNewMod;
|
||||
LoadModFromFileCommand.ExecuteEvent += LoadModFromFile;
|
||||
ClearHistoriesCommand.ExecuteEvent += ClearHistories;
|
||||
HistoriesFilterText.ValueChanged += ModFilterText_ValueChanged;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user