共用方式為


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 功能表,這是 Microsoft Excel 等 MDI) 應用程式 (MDI) 應用程式通用的屬性,或 (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 物件的執行緒取得。

適用於

另請參閱