This commit is contained in:
Sakura 2023-11-22 23:55:50 +08:00
parent 25ce5d78ba
commit 313f8fd79b
8 changed files with 110 additions and 38 deletions

View File

@ -65,6 +65,16 @@ namespace CPF.Toolkit.Demo
Content = "关闭窗体",
Commands = { { nameof(Button.Click),(s,e) => vm.Test() } }
},
new Button
{
Content = "关闭窗体",
Commands = { { nameof(Button.Click),(s,e) => vm.Test() } }
},
new Button
{
Content = "loading",
Commands = { { nameof(Button.Click),(s,e) => vm.LoadingTest() } }
},
}
}));
}

View File

@ -24,5 +24,18 @@ namespace CPF.Toolkit.Demo
e.Cancel = !this.isClose;
base.OnClose(e);
}
public async void LoadingTest()
{
await this.ShowLoading(Task.Delay(3000));
this.Dialog.Sucess("test");
//var result = await this.ShowLoading(async () =>
//{
// await Task.Delay(5000);
// return "test";
//});
//this.Dialog.Sucess(result);
}
}
}

View File

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

View File

@ -97,38 +97,25 @@ namespace CPF.Toolkit.Dialogs
{ nameof(TextBox.Text),nameof(Text),this,BindingMode.OneWay}
}
}.Assign(out var textBox),
//new Border
//{
// Attacheds = { { Grid.RowIndex,2 } },
// BorderThickness = new Thickness(0,1,0,0),
// BorderType = BorderType.BorderThickness,
// BorderFill = "236,236,236",
// Size = SizeField.Fill,
// MarginBottom = 5,
//},
new StackPanel
{
Height = "100%",
Attacheds = { { Grid.RowIndex,2 } },
MarginBottom = 4,
Orientation = Orientation.Horizontal,
}.Assign(out var p),
}
.LoopCreate(this.Buttons.Length, i => new Button
{
Content = this.Buttons[i],
MinWidth = this.Buttons.Length <= 1 ? 80 : 65,
Background = "white",
BorderFill = "236,236,236",
Height = "95%",
MarginRight = 5,
Commands = { { nameof(Button.Click),(s,e) => this.DialogResult = i } }
}),
}
}));
this.Buttons.Select(x => new Button
{
Content = x,
MinWidth = this.Buttons.Length <= 1 ? 80 : 65,
Background = "white",
BorderFill = "236,236,236",
Height = "95%",
MarginRight = 5,
}).ToList().ForEach(c =>
{
p.Children.Add(c);
c.Click += Button_Click;
});
textBox.TextChanged += TextBox_TextChanged;
}
@ -147,12 +134,6 @@ namespace CPF.Toolkit.Dialogs
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = (sender as Button).Content;
this.Close();
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.Key.Or(Keys.Enter, Keys.Space))

View File

@ -8,7 +8,7 @@ namespace CPF.Toolkit.Dialogs
{
internal interface IClosable
{
event EventHandler<bool?> Closable;
event EventHandler<object> Closable;
void OnClosable(object sender, ClosingEventArgs e);
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace CPF.Toolkit.Dialogs
{
public interface ILoading
{
event Func<string, Task, Task<object>> ShowLoadingFunc;
event Func<string, Task,Task> ShowLoading;
}
}

View File

@ -3,6 +3,7 @@ using CPF.Toolkit.Dialogs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace CPF.Toolkit
{
@ -36,7 +37,42 @@ namespace CPF.Toolkit
{
dialog.Dialog = new DialogService(view);
}
if (view.DataContext is ILoading loading)
{
loading.ShowLoadingFunc += async (message, task) =>
{
var loadingBox = new LoadingBox { Message = message };
var layer = new LayerDialog
{
Name = "loadingDialog",
Content = loadingBox,
ShowCloseButton = false,
Background = null,
};
layer.ShowDialog(view);
dynamic t = task;
var result = await t;
loadingBox.Invoke(layer.CloseDialog);
return (object)result;
};
loading.ShowLoading += async (message, task) =>
{
var loadingBox = new LoadingBox { Message = message };
var layer = new LayerDialog
{
Name = "loadingDialog",
Content = loadingBox,
ShowCloseButton = false,
Background = null,
};
layer.ShowDialog(view);
await task;
loadingBox.Invoke(layer.CloseDialog);
};
}
}
}
private static void View_Closing(object sender, ClosingEventArgs e)

View File

@ -3,18 +3,24 @@ using CPF.Toolkit.Dialogs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace CPF.Toolkit
{
public class ViewModelBase : ObservableObject, IClosable, IDialog
public class ViewModelBase : ObservableObject, IClosable, IDialog, ILoading
{
event EventHandler<bool?> _close;
event EventHandler<object> _close;
event Func<string, Task, Task<object>> _showLoadingFunc;
event Func<string, Task, Task> _showLading;
event EventHandler<object> IClosable.Closable { add => this._close += value; remove => this._close -= value; }
event Func<string, Task, Task<object>> ILoading.ShowLoadingFunc { add => this._showLoadingFunc += value; remove => this._showLoadingFunc -= value; }
event Func<string, Task, Task> ILoading.ShowLoading { add => this._showLading += value; remove => this._showLading -= value; }
void IClosable.OnClosable(object sender, ClosingEventArgs e) => this.OnClose(e);
public IDialogService Dialog { get; set; }
event EventHandler<bool?> IClosable.Closable { add => this._close += value; remove => this._close -= value; }
protected void Close(bool? dialogResult = null)
protected void Close(object dialogResult = null)
{
if (this._close == null)
{
@ -25,6 +31,17 @@ namespace CPF.Toolkit
protected virtual void OnClose(ClosingEventArgs e) { }
void IClosable.OnClosable(object sender, ClosingEventArgs e) => this.OnClose(e);
protected async Task ShowLoading(Task task)
{
if (this._showLading == null) throw new ArgumentNullException();
await this._showLading.Invoke("加载中……", task);
}
protected async Task<T> ShowLoading<T>(Func<Task<T>> task)
{
if (this._showLoadingFunc == null) throw new ArgumentNullException();
var result = await this._showLoadingFunc.Invoke("加载中……", task.Invoke());
return (T)result;
}
}
}