using AIK.Service.IService; using Microsoft.Xaml.Behaviors; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AIK.Behaviors { public class ViewModelInitializationWindowBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += OnViewLoaded; AssociatedObject.Loaded += OnViewModelRequestClose; } protected override void OnDetaching() { AssociatedObject.Loaded -= OnViewLoaded; AssociatedObject.Loaded -= OnViewModelRequestClose; base.OnDetaching(); } private async void OnViewLoaded(object sender, RoutedEventArgs e) { if (AssociatedObject.DataContext is ILoadAware loadAware) { await loadAware.OnLoadedAsync(); } } private void OnViewModelRequestClose(object? sender, EventArgs e) { AssociatedObject?.Dispatcher.Invoke(() => { if (AssociatedObject.IsVisible) AssociatedObject.Close(); }); } } }