feat: add mvvm demo.

This commit is contained in:
Dong Bin
2025-02-24 19:15:55 +08:00
parent b20137e8aa
commit aa4507c9c8
5 changed files with 138 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Irihi.Avalonia.Shared.Contracts;
namespace Ursa.Controls;
@@ -18,6 +19,24 @@ public abstract class SplashWindow: Window
get => GetValue(CountDownProperty);
set => SetValue(CountDownProperty, value);
}
static SplashWindow()
{
DataContextProperty.Changed.AddClassHandler<SplashWindow, object?>((window, e) =>
window.OnDataContextChange(e));
}
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
{
if (args.OldValue.Value is IDialogContext oldContext) oldContext.RequestClose -= OnContextRequestClose;
if (args.NewValue.Value is IDialogContext newContext) newContext.RequestClose += OnContextRequestClose;
}
private void OnContextRequestClose(object? sender, object? args)
{
Close(args);
}
protected override void OnLoaded(RoutedEventArgs e)
{
@@ -49,6 +68,11 @@ public abstract class SplashWindow: Window
}
nextWindow.Show();
Close();
if (DataContext is IDialogContext idc)
{
idc.Close();
idc.RequestClose -= OnContextRequestClose;
}
return;
}
}