Application.Windows 属性

定义

获取应用程序中的实例化窗口。

public:
 property System::Windows::WindowCollection ^ Windows { System::Windows::WindowCollection ^ get(); };
public System.Windows.WindowCollection Windows { get; }
member this.Windows : System.Windows.WindowCollection
Public ReadOnly Property Windows As WindowCollection

属性值

一个 WindowCollection,包含对当前 AppDomain 中的所有窗口对象的引用。

示例

以下示例演示如何枚举 Windows 属性以生成顶级 Windows 菜单,该菜单常见于多文档接口 (MDI) 应用程序(如 Microsoft Excel)或多实例单文档接口 (SDI) 应用程序(如 Microsoft Word)。

using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    // Custom menu item that stores a reference to a window
    public class WindowMenuItem : MenuItem
    {
        public Window Window = null;
    }
}

Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    ' Custom menu item that stores a reference to a window
    Public Class WindowMenuItem
        Inherits MenuItem
        Public Window As Window = Nothing
    End Class
End Namespace
<Window x:Class="CSharp.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="CSharp" Height="300" Width="300" Activated="MainWindow_Activated"
    >
  <StackPanel>
    <Menu>
      <MenuItem Header="_File">
        <MenuItem Name="newWindowMenuItem" Click="newWindowMenuItem_Click" Header="_New Window"></MenuItem>
        <Separator></Separator>
        <MenuItem Name="exitMenuItem" Click="exitMenuItem_Click" Header="E_xit"></MenuItem>
      </MenuItem>
      <MenuItem Name="windowMenuItem" Header="_Window">
      </MenuItem>
    </Menu>
    <Canvas></Canvas>
  </StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
        End Sub
        void MainWindow_Activated(object sender, EventArgs e)
        {
            this.windowMenuItem.Items.Clear();
            int windowCount = 0;
            foreach (Window window in Application.Current.Windows)
            {
                ++windowCount;
                WindowMenuItem menuItem = new WindowMenuItem();
                menuItem.Window = window;
                menuItem.Header = "_" + windowCount.ToString() + " Window " + windowCount.ToString();
                menuItem.Click += new RoutedEventHandler(menuItem_Click);
                this.windowMenuItem.Items.Add(menuItem);
            }
        }

        void menuItem_Click(object sender, RoutedEventArgs e)
        {
            WindowMenuItem menuItem = (WindowMenuItem)sender;
            menuItem.Window.Activate();
        }
    }
}
        Private Sub MainWindow_Activated(ByVal sender As Object, ByVal e As EventArgs)
            Me.windowMenuItem.Items.Clear()
            Dim windowCount As Integer = 0
            For Each window As Window In Application.Current.Windows
                windowCount += 1
                Dim menuItem As New WindowMenuItem()
                menuItem.Window = window
                menuItem.Header = "_" & windowCount.ToString() & " Window " & windowCount.ToString()
                AddHandler menuItem.Click, AddressOf menuItem_Click
                Me.windowMenuItem.Items.Add(menuItem)
            Next window
        End Sub

        Private Sub menuItem_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim menuItem As WindowMenuItem = CType(sender, WindowMenuItem)
            menuItem.Window.Activate()
        End Sub
    End Class
End Namespace

注解

Window在用户界面 (UI) 线程上实例化窗口后,将自动添加Windows引用;不会添加由工作线程创建的窗口。 引用 Window 在处理其 Closing 事件后以及引发其事件之前,会自动删除该 Closed 引用。

默认情况下,添加到 Windows 属性的第一个项将成为 MainWindow

此属性仅在创建 对象的线程中 Application 可用。

适用于

另请参阅