feat: WIP
This commit is contained in:
@@ -25,7 +25,7 @@ public class DialogDemoViewModel: ObservableObject
|
||||
|
||||
private async Task ShowGlobalDialog()
|
||||
{
|
||||
await DialogBox.ShowAsync<BadgeDemo, BadgeDemoViewModel, string>(new BadgeDemoViewModel());
|
||||
var result = await DialogBox.ShowAsync<ButtonGroupDemo, ButtonGroupDemoViewModel, string>(new ButtonGroupDemoViewModel());
|
||||
}
|
||||
|
||||
private async Task ShowGlobalOverlayDialog()
|
||||
|
||||
@@ -8,14 +8,12 @@ namespace Ursa.Controls;
|
||||
|
||||
public static class DialogBox
|
||||
{
|
||||
public static async Task<TResult?> ShowAsync<TView, TViewModel, TResult>(TViewModel vm) where TView : Control, new()
|
||||
public static async Task<TResult?> ShowAsync<TView, TViewModel, TResult>(TViewModel vm)
|
||||
where TView : Control, new()
|
||||
{
|
||||
var window = new DialogWindow()
|
||||
{
|
||||
Content = new TView()
|
||||
{
|
||||
DataContext = vm,
|
||||
},
|
||||
Content = new TView(),
|
||||
DataContext = vm,
|
||||
};
|
||||
var lifetime = Application.Current?.ApplicationLifetime;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -13,6 +14,24 @@ public class DialogWindow: Window
|
||||
|
||||
private Button? _closeButton;
|
||||
|
||||
protected override void OnDataContextBeginUpdate()
|
||||
{
|
||||
base.OnDataContextBeginUpdate();
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
context.Closed-= OnDialogClose;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDataContextEndUpdate()
|
||||
{
|
||||
base.OnDataContextEndUpdate();
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
context.Closed += OnDialogClose;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
@@ -20,11 +39,24 @@ public class DialogWindow: Window
|
||||
|
||||
if (_closeButton != null)
|
||||
{
|
||||
_closeButton.Click += (sender, args) =>
|
||||
{
|
||||
Close();
|
||||
};
|
||||
_closeButton.Click += OnDefaultClose;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDialogClose(object? sender, object? args)
|
||||
{
|
||||
Close(args);
|
||||
}
|
||||
|
||||
private void OnDefaultClose(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
Close(context.DefaultCloseResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
Close(null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,6 @@ namespace Ursa.Controls;
|
||||
|
||||
public interface IDialogContext
|
||||
{
|
||||
object Close();
|
||||
T? Close<T>();
|
||||
object? DefaultCloseResult { get; set; }
|
||||
event EventHandler<object> Closed;
|
||||
}
|
||||
Reference in New Issue
Block a user