Application.Windows Application.Windows Application.Windows Application.Windows Property

Definition

Ruft die instanziierten Fenster in einer Anwendung ab.Gets the instantiated windows in an application.

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

Eigenschaftswert

Eine WindowCollection, die Verweise auf alle Fensterobjekte in der aktuellen AppDomain enthält.A WindowCollection that contains references to all window objects in the current AppDomain.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie die Windows -Eigenschaft aufgelistet wird, um ein Windows-Menü auf oberster Ebene zu erstellen, das von Schnittstelle für mehrere Dokumente (Multiple-Document Interfacce, MDI)multiple-document interface (MDI) Anwendungen wie Microsoft ExcelMicrosoft Exceloder mehreren Instanzen von SDI-Anwendungen (Single Document Interface) wie Microsoft WordMicrosoft Word.The following example demonstrates how to enumerate the Windows property to build a top-level Windows menu, which is common to Schnittstelle für mehrere Dokumente (Multiple-Document Interfacce, MDI)multiple-document interface (MDI) applications like Microsoft ExcelMicrosoft Excel, or multiple-instance Single Document Interface (SDI) applications like Microsoft WordMicrosoft 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

Hinweise

Ein Window Verweis wird automatisch hinzugefügt Windows , sobald ein Fenster im Benutzeroberfläche (User Interface, UI)user interface (UI) Thread instanziiert wird. Windows, die von Arbeitsthreads erstellt werden, werden nicht hinzugefügt.A Window reference is automatically added to Windows as soon as a window is instantiated on the Benutzeroberfläche (User Interface, UI)user interface (UI) thread; windows that are created by worker threads are not added. Ein Window Verweis wird automatisch entfernt, nachdem Closing das-Ereignis behandelt wurde und bevor Closed das-Ereignis ausgelöst wird.A Window reference is automatically removed after its Closing event has been handled and before its Closed event is raised.

Standardmäßig Windows MainWindowwird das erste Element, das der-Eigenschaft hinzugefügt wird, zur.By default, the first item added to the Windows property becomes the MainWindow.

Diese Eigenschaft ist nur in dem Thread verfügbar, von dem Application das Objekt erstellt wurde.This property is available only from the thread that created the Application object.

Gilt für:

Siehe auch