From 313f8fd79be3d6caf33dcedeabf76d2e4a8360ed Mon Sep 17 00:00:00 2001 From: Sakura Date: Wed, 22 Nov 2023 23:55:50 +0800 Subject: [PATCH] ILoading --- CPF.Toolkit.Demo/MainView.cs | 10 ++++++++ CPF.Toolkit.Demo/MainViewModel.cs | 13 ++++++++++ CPF.Toolkit/CPF.Toolkit.csproj | 4 ++- CPF.Toolkit/Dialogs/DialogView.cs | 41 +++++++++---------------------- CPF.Toolkit/Dialogs/IClosable.cs | 2 +- CPF.Toolkit/Dialogs/ILoading.cs | 13 ++++++++++ CPF.Toolkit/ViewManager.cs | 36 +++++++++++++++++++++++++++ CPF.Toolkit/ViewModelBase.cs | 29 +++++++++++++++++----- 8 files changed, 110 insertions(+), 38 deletions(-) create mode 100644 CPF.Toolkit/Dialogs/ILoading.cs diff --git a/CPF.Toolkit.Demo/MainView.cs b/CPF.Toolkit.Demo/MainView.cs index ea82b52..b385519 100644 --- a/CPF.Toolkit.Demo/MainView.cs +++ b/CPF.Toolkit.Demo/MainView.cs @@ -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() } } + }, } })); } diff --git a/CPF.Toolkit.Demo/MainViewModel.cs b/CPF.Toolkit.Demo/MainViewModel.cs index 0aa3915..efe9c4d 100644 --- a/CPF.Toolkit.Demo/MainViewModel.cs +++ b/CPF.Toolkit.Demo/MainViewModel.cs @@ -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); + } } } diff --git a/CPF.Toolkit/CPF.Toolkit.csproj b/CPF.Toolkit/CPF.Toolkit.csproj index a49aac9..4e089e0 100644 --- a/CPF.Toolkit/CPF.Toolkit.csproj +++ b/CPF.Toolkit/CPF.Toolkit.csproj @@ -21,5 +21,7 @@ - + + + diff --git a/CPF.Toolkit/Dialogs/DialogView.cs b/CPF.Toolkit/Dialogs/DialogView.cs index d1a76ee..8a752b0 100644 --- a/CPF.Toolkit/Dialogs/DialogView.cs +++ b/CPF.Toolkit/Dialogs/DialogView.cs @@ -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)) diff --git a/CPF.Toolkit/Dialogs/IClosable.cs b/CPF.Toolkit/Dialogs/IClosable.cs index fec269a..374004d 100644 --- a/CPF.Toolkit/Dialogs/IClosable.cs +++ b/CPF.Toolkit/Dialogs/IClosable.cs @@ -8,7 +8,7 @@ namespace CPF.Toolkit.Dialogs { internal interface IClosable { - event EventHandler Closable; + event EventHandler Closable; void OnClosable(object sender, ClosingEventArgs e); } } diff --git a/CPF.Toolkit/Dialogs/ILoading.cs b/CPF.Toolkit/Dialogs/ILoading.cs new file mode 100644 index 0000000..ed8d6b0 --- /dev/null +++ b/CPF.Toolkit/Dialogs/ILoading.cs @@ -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> ShowLoadingFunc; + event Func ShowLoading; + } +} diff --git a/CPF.Toolkit/ViewManager.cs b/CPF.Toolkit/ViewManager.cs index 3ff3f97..6eccb24 100644 --- a/CPF.Toolkit/ViewManager.cs +++ b/CPF.Toolkit/ViewManager.cs @@ -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) diff --git a/CPF.Toolkit/ViewModelBase.cs b/CPF.Toolkit/ViewModelBase.cs index 5529bbf..34a2d2b 100644 --- a/CPF.Toolkit/ViewModelBase.cs +++ b/CPF.Toolkit/ViewModelBase.cs @@ -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 _close; + event EventHandler _close; + event Func> _showLoadingFunc; + event Func _showLading; + event EventHandler IClosable.Closable { add => this._close += value; remove => this._close -= value; } + event Func> ILoading.ShowLoadingFunc { add => this._showLoadingFunc += value; remove => this._showLoadingFunc -= value; } + event Func 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 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 ShowLoading(Func> task) + { + if (this._showLoadingFunc == null) throw new ArgumentNullException(); + var result = await this._showLoadingFunc.Invoke("加载中……", task.Invoke()); + return (T)result; + } } }