兼容win7版应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

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<Window>
{
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();
});
}
}
}