CPF/CPF.Toolkit/ViewManager.cs

53 lines
1.5 KiB
C#
Raw Normal View History

using CPF.Controls;
using CPF.Toolkit.Dialogs;
using System;
using System.Collections.Generic;
using System.Text;
2023-11-22 23:55:50 +08:00
using System.Threading.Tasks;
namespace CPF.Toolkit
{
public static class ViewManager
{
public static T View<T>(params object[] arges) where T : Window
{
var view = Activator.CreateInstance(typeof(T), arges) as T;
view.Closing += View_Closing;
2023-11-22 21:28:58 +08:00
view.PropertyChanged += View_PropertyChanged;
return view;
}
2023-11-22 21:28:58 +08:00
private static void View_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
{
var view = sender as Window;
2023-11-22 21:28:58 +08:00
if (e.PropertyName == nameof(Window.DataContext))
{
2023-11-22 21:28:58 +08:00
if (view.DataContext is IClosable closable)
{
2023-11-29 17:07:17 +08:00
closable.Closable += (ss, ee) =>
2023-11-22 21:28:58 +08:00
{
view.Close();
};
}
if (view.DataContext is IDialog dialog)
{
dialog.Dialog = new DialogService(view);
}
2023-11-22 23:55:50 +08:00
if (view.DataContext is ILoading loading)
{
2023-11-29 17:07:17 +08:00
loading.CreateLoading(view);
2023-11-22 23:55:50 +08:00
}
}
}
2023-11-22 21:28:58 +08:00
private static void View_Closing(object sender, ClosingEventArgs e)
{
var view = sender as Window;
if (view.DataContext is IClosable closable)
{
2023-11-22 21:28:58 +08:00
closable.OnClosable(sender, e);
}
}
}
}