From c9698428701faa416c789ed05772f364a93b4175 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 22 Jan 2024 19:55:20 +0800 Subject: [PATCH] feat: set result. --- .../ViewModels/DialogDemoViewModel.cs | 4 +-- src/Ursa/Controls/Dialog/DialogControl.cs | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs index 02ad255..05df4a7 100644 --- a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs @@ -42,13 +42,13 @@ public class DialogDemoViewModel: ObservableObject private async Task ShowGlobalDialog() { - var result = await DialogBox.ShowAsync(new DialogWithActionViewModel()); + var result = await DialogBox.ShowAsync(DialogViewModel); Result = result; } private async Task ShowGlobalOverlayDialog() { - await DialogBox.ShowOverlayAsync(new DialogWithActionViewModel(), "GlobalHost"); + Result = await DialogBox.ShowOverlayAsync(DialogViewModel, "GlobalHost"); } private async Task ShowLocalOverlayDialog() diff --git a/src/Ursa/Controls/Dialog/DialogControl.cs b/src/Ursa/Controls/Dialog/DialogControl.cs index 5e1f408..2436104 100644 --- a/src/Ursa/Controls/Dialog/DialogControl.cs +++ b/src/Ursa/Controls/Dialog/DialogControl.cs @@ -1,3 +1,4 @@ +using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; @@ -11,7 +12,26 @@ public class DialogControl: ContentControl public const string PART_CloseButton = "PART_CloseButton"; private Button? _closeButton; - public event EventHandler OnClose; + public event EventHandler? OnClose; + + static DialogControl() + { + DataContextProperty.Changed.AddClassHandler((o, e) => o.OnDataContextChange(e)); + } + + private void OnDataContextChange(AvaloniaPropertyChangedEventArgs args) + { + if (args.OldValue.Value is IDialogContext oldContext) + { + oldContext.Closed-= Close; + } + + if (args.NewValue.Value is IDialogContext newContext) + { + newContext.Closed += Close; + } + + } protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { @@ -25,11 +45,6 @@ public class DialogControl: ContentControl { _closeButton.Click += Close; } - - if (this.DataContext is IDialogContext context) - { - context.Closed += Close; - } }