Toolkit.Avalonia
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.Avalonia;
|
||||
|
||||
public class ContentControlHandler(INavigationContext navigationContext) :
|
||||
INavigateHandler<ContentControl>
|
||||
{
|
||||
public async Task Handle(Navigate<ContentControl> args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (args.Context is ContentControl contentControl)
|
||||
{
|
||||
if (args.Template is Control control)
|
||||
{
|
||||
TaskCompletionSource taskCompletionSource = new();
|
||||
async void HandleLoaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
control.Loaded -= HandleLoaded;
|
||||
if (control.DataContext is object content)
|
||||
{
|
||||
if (content is IInitializer initializer)
|
||||
{
|
||||
await initializer.Initialize();
|
||||
}
|
||||
|
||||
if (content is IActivated activated)
|
||||
{
|
||||
await activated.Activated();
|
||||
}
|
||||
}
|
||||
|
||||
taskCompletionSource.SetResult();
|
||||
}
|
||||
|
||||
async void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
control.Unloaded -= HandleLoaded;
|
||||
if (control.DataContext is object content)
|
||||
{
|
||||
if (content is IDeactivated deactivated)
|
||||
{
|
||||
await deactivated.Deactivated();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.Loaded += HandleLoaded;
|
||||
control.Unloaded += HandleUnloaded;
|
||||
|
||||
contentControl.Content = control;
|
||||
contentControl.DataContext = args.Content;
|
||||
|
||||
navigationContext.Set(control);
|
||||
await taskCompletionSource.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user