From ce93f34467864b64481fffa33bd43b6cf54e4806 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 3 Apr 2019 22:07:17 +0800 Subject: [PATCH] update pr/58 --- HandyControl/Controls/Other/Dialog.cs | 13 ++++-- .../Tools/Extension/DialogExtension.cs | 45 ++++++------------- .../UserControl/Basic/InteractiveDialog.xaml | 29 +++--------- .../Basic/InteractiveDialog.xaml.cs | 3 -- .../UserControl/Controls/DialogDemoCtl.xaml | 25 +++++------ .../Basic/InteractiveDialogViewModel.cs | 30 +++++-------- .../ViewModel/Controls/DialogDemoViewModel.cs | 31 ++++++------- .../ViewModel/ViewModelLocator.cs | 1 - 8 files changed, 65 insertions(+), 112 deletions(-) diff --git a/HandyControl/Controls/Other/Dialog.cs b/HandyControl/Controls/Other/Dialog.cs index ec17afec..9810bb6e 100644 --- a/HandyControl/Controls/Other/Dialog.cs +++ b/HandyControl/Controls/Other/Dialog.cs @@ -1,4 +1,5 @@ -using System.Windows.Controls; +using System.Windows; +using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using HandyControl.Interactivity; @@ -10,8 +11,14 @@ namespace HandyControl.Controls { private Adorner _container; - // It can not be replaced with the FrameworkElement.IsLoaded property, which will be false when the dialog just created. - public bool IsClosed { get; private set; } + public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register( + "IsClosed", typeof(bool), typeof(Dialog), new PropertyMetadata(default(bool))); + + public bool IsClosed + { + get => (bool) GetValue(IsClosedProperty); + internal set => SetValue(IsClosedProperty, value); + } public Dialog() { diff --git a/HandyControl/Tools/Extension/DialogExtension.cs b/HandyControl/Tools/Extension/DialogExtension.cs index ab536e3e..aae15918 100644 --- a/HandyControl/Tools/Extension/DialogExtension.cs +++ b/HandyControl/Tools/Extension/DialogExtension.cs @@ -5,29 +5,29 @@ using HandyControl.Controls; namespace HandyControl.Tools.Extension { - public interface IDialogResultable + public interface IDialogResultable { - object Result { get; set; } + T Result { get; set; } - Action ClosureToken { get; set; } + Action CloseAction { get; set; } } public static class DialogExtension { - public static Task GetResultAsync(this Dialog @this) + public static Task GetResultAsync(this Dialog dialog) { var tcs = new TaskCompletionSource(); try { - if (@this.IsClosed) + if (dialog.IsClosed) { SetResult(); } else { - @this.Unloaded += OnUnloaded; - @this.GetViewModel().ClosureToken = @this.Close; + dialog.Unloaded += OnUnloaded; + dialog.GetViewModel>().CloseAction = dialog.Close; } } catch (Exception e) @@ -40,7 +40,7 @@ namespace HandyControl.Tools.Extension // ReSharper disable once ImplicitlyCapturedClosure void OnUnloaded(object sender, RoutedEventArgs args) { - @this.Unloaded -= OnUnloaded; + dialog.Unloaded -= OnUnloaded; SetResult(); } @@ -48,7 +48,7 @@ namespace HandyControl.Tools.Extension { try { - tcs.TrySetResult(ExtractResult(@this)); + tcs.TrySetResult(dialog.GetViewModel>().Result); } catch (Exception e) { @@ -57,16 +57,16 @@ namespace HandyControl.Tools.Extension } } - public static Dialog Initialize(this Dialog @this, Action configure) + public static Dialog Initialize(this Dialog dialog, Action configure) { - configure?.Invoke(@this.GetViewModel()); + configure?.Invoke(dialog.GetViewModel()); - return @this; + return dialog; } - public static TViewModel GetViewModel(this Dialog @this) + public static TViewModel GetViewModel(this Dialog dialog) { - if (!(@this.Content is FrameworkElement frameworkElement)) + if (!(dialog.Content is FrameworkElement frameworkElement)) throw new InvalidOperationException("The dialog is not a derived class of the FrameworkElement. "); if (!(frameworkElement.DataContext is TViewModel viewModel)) @@ -74,22 +74,5 @@ namespace HandyControl.Tools.Extension return viewModel; } - - private static TResult ExtractResult(Dialog dialog) - { - var viewModelResult = dialog.GetViewModel().Result; - - switch (viewModelResult) - { - case TResult result: - return result; - case null when !typeof(TResult).IsValueType: - return default(TResult); // The default value of the value type may cause confusion, so should not help the user decide. - default: - throw new InvalidCastException("Could not cast " + - $"the {(viewModelResult != null ? $"{viewModelResult.GetType()} type" : "null value")} " + - $"to the {typeof(TResult)} type. "); - } - } } } diff --git a/HandyControlDemo/UserControl/Basic/InteractiveDialog.xaml b/HandyControlDemo/UserControl/Basic/InteractiveDialog.xaml index c2e488c7..cc3bdb12 100644 --- a/HandyControlDemo/UserControl/Basic/InteractiveDialog.xaml +++ b/HandyControlDemo/UserControl/Basic/InteractiveDialog.xaml @@ -3,31 +3,14 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs" xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl" - xmlns:interactivity="clr-namespace:HandyControl.Interactivity;assembly=HandyControl" CornerRadius="10" Width="400" Height="247" - DataContext="{Binding Path=InteractiveDialog, Source={StaticResource Locator}}" + DataContext="{Binding InteractiveDialog, Source={StaticResource Locator}}" Background="{DynamicResource RegionBrush}"> - - - - -