在贴吧上看到这么个需求,就是需要能够关闭最后一个处于激活状态的窗体
所以写了这么个小类
/// <summary> /// 窗体管理 /// </summary> public class WindowManager { private static Lazy<WindowManager> manager = new Lazy<WindowManager>(() => new WindowManager()); public static WindowManager Manager { get { return manager.Value; } } /// <summary> /// 存储所有加入的激活窗体 /// </summary> private List<Window> lastWindows = new List<Window>(); /// <summary> /// 将窗体添加到管理中 /// </summary> /// <param name="window">要添加的窗体</param> public void AddActive(Window window) { //绑定窗体的激活事件 window.Activated += Window_Activated; //绑定窗体的关闭事件 window.Closing += Window_Closing; //如果添加的新窗体已加载,则添加到激活列表中 if (window.IsLoaded) this.lastWindows.Add(window); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { this.RemoveActive(sender); } private Window RemoveActive(object sender) { var window = sender as Window; //关闭,则从队列中移除 if (this.lastWindows.Contains(window)) { this.lastWindows.Remove(window); } return window; } private void Window_Activated(object sender, EventArgs e) { var window = this.RemoveActive(sender); this.lastWindows.Add(window); } /// <summary> /// 关闭最后一个激活的窗体 /// </summary> public void CloseActive() { //获取最后一个,直接关闭 var window = this.lastWindows.LastOrDefault(); if (window != null) { window.Close(); } } /// <summary> /// 获取最后一个激活的窗体 /// </summary> /// <returns></returns> public Window GetLastActive() { return this.lastWindows.LastOrDefault(); } /// <summary> /// 关闭所有已激活的窗体 /// </summary> public void CloseAll() { for (int i = this.lastWindows.Count - 1; i > -1; i--) { this.lastWindows[i].Close(); } } }
使用方式
Xaml
<StackPanel> <Button Content="Open" Click="Open_Click"/> <Button Content="Get" Click="Get_Click"/> <Button Content="Close" Click="Close_Click"/> <Button Content="Close All" Click="CloseAll_Click"/> <TextBox x:Name="txt" TextWrapping="Wrap" Height="300" VerticalScrollBarVisibility="Auto" /> </StackPanel>
后台事件
private void Open_Click(object sender, RoutedEventArgs e) { var window= new Window1(Guid.NewGuid().ToString()); WindowManager.Manager.AddActive(window); window.Show(); } private void Get_Click(object sender, RoutedEventArgs e) { var window = WindowManager.Manager.GetLastActive(); if (window != null) this.txt.Text += window.Title + " :Active" + Environment.NewLine; } private void Close_Click(object sender, RoutedEventArgs e) { WindowManager.Manager.CloseActive(); } private void CloseAll_Click(object sender, RoutedEventArgs e) { WindowManager.Manager.CloseAll(); }
开启新窗体时,以其Title来标识不同的窗体
/// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { public Window1(string title) { InitializeComponent(); this.Title = title; } }
最后效果gif