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