Window
提供创建、配置、显示和管理窗口和对话框的生存期的能力。
用户与独立应用程序之间的交互点是一个窗口。 WPF窗口 (Windows Presentation Foundation) 包括两个不同的区域:
- 承载 windows 修饰的非工作区,包括图标、标题、系统菜单、最小化按钮、最大化按钮、还原按钮、关闭按钮和边框。
- 承载特定于应用程序的内容的工作区。
标准窗口如下图所示:
Window 封装了创建、配置、显示和管理窗口和对话框的生存期的功能,并提供了以下关键服务:
生存期管理: Activate 、 Activated 、 Close 、Closed、 Closing 、Deactivated、 Hide、 IsActive、 Show 、 SourceInitialized 。
窗口管理: GetWindow 、 OwnedWindows 、 Owner 。
外观和行为: AllowsTransparency 、 ContentRendered 、 DragMove 、 Icon 、 Left 、 LocationChanged 、ResizeMode、 RestoreBounds 、ShowActivated 、ShowInTaskbar、 SizeToContent 、StateChanged、 Title、 Top 、Topmost、 WindowStartupLocation 、WindowState 、 WindowStyle
对话框: DialogResult 、 ShowDialog 。
此外,还 Application 公开了对管理应用程序中所有窗口的特殊支持:
- 应用程序维护当前在应用程序中实例化的所有窗口的列表。 此列表由 Windows 属性公开。
- 默认情况下, MainWindow 将使用对 Window 应用程序中实例化的第一个的引用自动设置。 从而使窗口成为主应用程序窗口。
Window可以使用标记、标记和代码隐藏或代码来实现。
Window 主要用于显示独立应用程序的窗口和对话框。 但是,对于需要在窗口级别导航的应用程序(如向导),可以改为使用 NavigationWindow ; NavigationWindow 从派生 Window ,并使用浏览器样式的导航支持对其进行扩展。
Window 需要 UnmanagedCode
实例化安全权限。 这会产生以下影响:
- 当从 Internet 或本地 Intranet 区域启动时,ClickOnce 部署的独立应用程序将请求权限提升。
- 请求小于完全权限的 Xbap 将不能实例化 windows 或对话框。
有关独立应用程序部署和安全注意事项的信息,请参阅 WPF 安全策略-平台安全性。
Window是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。 另外, Window 是根元素,因此,不能是另一个元素的内容的一部分。
名称 | 备注 | 权限 |
---|---|---|
AllowsTransparencyProperty | 标识 AllowsTransparency 依赖项属性。 | public static readonly |
DpiChangedEvent | 显示该窗口的屏幕的 DPI 发生更改时使用的 RoutedEvent。 | public static readonly |
IconProperty | 标识 Icon 依赖项属性。 | public static readonly |
IsActiveProperty | 标识 IsActive 依赖项属性。 | public static readonly |
LeftProperty | 标识 Left 依赖项属性。 | public static readonly |
ResizeModeProperty | 标识 ResizeMode 依赖项属性。 | public static readonly |
ShowActivatedProperty | 标识 ShowActivated 依赖项属性。 | public static readonly |
ShowInTaskbarProperty | 标识 ShowInTaskbar 依赖项属性。 | public static readonly |
SizeToContentProperty | 标识 SizeToContent 依赖项属性。 | public static readonly |
TaskbarItemInfoProperty | 标识 TaskbarItemInfo 依赖项属性。 | public static readonly |
TitleProperty | 标识 Title 依赖项属性。 | public static readonly |
TopmostProperty | 标识 Topmost 依赖项属性。 | public static readonly |
TopProperty | 标识 Top 依赖项属性。 | public static readonly |
WindowStateProperty | 标识 WindowState 依赖项属性。 | public static readonly |
WindowStyleProperty | 标识 WindowStyle 依赖项属性。 | public static readonly |
名称 | 备注 | 权限 |
---|---|---|
AllowsTransparency | 获取或设置一个值,该值指示窗口的工作区是否支持透明。 | get; set; |
DialogResult | 获取或设置对话框结果值,此值是从 ShowDialog() 方法返回的值。 | get; set; |
Icon | 获取或设置窗口的图标。 | get; set; |
IsActive | 获取一个值,该值指示窗口是否为活动窗口。 | get; |
Left | 获取或设置窗口左边缘相对于桌面的位置。 | get; set; |
LogicalChildren | 获取窗口的逻辑子元素的一个枚举数。 | get; |
OwnedWindows | 获取此窗口所拥有的窗口的集合。 | get; |
Owner | 获取或设置拥有此 Window 的 Window。 | get; set; |
ResizeMode | 获取或设置调整大小模式。 | get; set; |
RestoreBounds | 获取窗口在最小化或最大化之前的大小和位置。 | get; |
ShowActivated | 获取或设置一个值,该值指示在第一次显示窗口时,窗口是否处于激活状态。 | get; set; |
ShowInTaskbar | 获取或设置一个指示窗口是否具有任务栏按钮的值。 | get; set; |
SizeToContent | 获取或设置一个值,该值指示窗口是否自动调整自身大小以适应其内容大小。 | get; set; |
TaskbarItemInfo | 获取或设置的 Windows 7 任务栏缩略图 Window 。 | get; set; |
Title | 获取或设置窗口的标题。 | get; set; |
Top | 获取或设置窗口上边缘相对于桌面的位置。 | get; set; |
Topmost | 获取或设置一个值,该值指示窗口是否出现在 Z 顺序的最顶层。 | get; set; |
WindowStartupLocation | 获取或设置窗口首次显示时的位置。 | get; set; |
WindowState | 获取或设置一个值,该值指示窗口是处于还原、最小化还是最大化状态。 | get; set; |
WindowStyle | 获取或设置窗口的边框样式。 | get; set; |
名称 | 备注 | 权限 |
---|---|---|
Activate | 尝试将窗口置于前台并激活它。 | |
ArrangeOverride | 重写此方法可对窗口及其子元素进行排列和大小调整。 | protected |
Close | 手动关闭 Window。 | public |
DragMove | 允许使用在窗口工作区的暴露区域上方按下其鼠标左键的鼠标来拖动窗口。 | public |
GetWindow | 返回对 Window 对象的引用,该对象承载依赖项对象所在的内容树。 | public |
Hide | 使窗口不可见。 | public |
MeasureOverride | 重写此方法可测量窗口的大小。 | protected |
OnActivated | 引发 Activated 事件。 | protected |
OnClosed | 引发 Closed 事件。 | protected |
OnClosing | 引发 Closing 事件。 | protected |
OnContentChanged | 当 Content 属性更改时调用。 | protected |
OnContentRendered | 引发 ContentRendered 事件。 | protected |
OnCreateAutomationPeer | 为此 WindowAutomationPeer 创建并返回一个 Window 对象。 | protected |
OnDeactivated | 引发 Deactivated 事件。 | protected |
OnDpiChanged | 呈现此窗口所使用的 DPI 发生变化时进行调用。 | protected |
OnLocationChanged | 引发 LocationChanged 事件。 | protected |
OnManipulationBoundaryFeedback | 在 ManipulationBoundaryFeedback 事件发生时调用。 | protected |
OnSourceInitialized | 引发 SourceInitialized 事件。 | protected |
OnStateChanged | 引发 StateChanged 事件。 | protected |
OnVisualChildrenChanged | 此 Window 对象的 VisualCollection 被修改时调用。 | protected |
OnVisualParentChanged | 在窗口的父项更改时调用。 | protected |
Show | 打开窗口并返回,而不等待新打开的窗口关闭。 | public |
ShowDialog | 打开一个窗口,并且仅在新打开的窗口关闭后才返回。 | public |
名称 | 备注 |
---|---|
Activated | 在窗口成为前台窗口时发生。 |
Closed | 在窗口即将关闭时发生。 |
Closing | 在调用 Close() 之后立即发生,并且可进行处理以取消关闭窗口。 |
ContentRendered | 在窗口的内容呈现完毕之后发生。 |
Deactivated | 在窗口成为后台窗口时发生。 |
DpiChanged | 显示窗口的屏幕的 DPI 发生更改后出现。 |
LocationChanged | 在窗口的位置改变时发生。 |
SourceInitialized | 引发此事件是为了支持与 Win32 的互操作。 请参阅 HwndSource。 |
StateChanged | 在窗口的 WindowState 属性更改时发生。 |
XAML代码
<Window
x:Class="WindowDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WindowDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="400"
Height="450"
AllowsTransparency="False"
Icon="Img/icon.png"
Left="0"
ResizeMode="CanResizeWithGrip"
ShowActivated="True"
ShowInTaskbar="True"
SizeToContent="Manual"
Top="0"
Topmost="False"
WindowStartupLocation="CenterOwner"
WindowState="Normal"
WindowStyle="SingleBorderWindow"
mc:Ignorable="d"
Activated="OnActivate"
Closed="OnClosed"
Closing="OnClosing"
ContentRendered="OnContentRendered"
Deactivated="OnDeactivated"
LocationChanged="OnLocationChanged"
StateChanged="OnStateChanged" >
<ScrollViewer>
<TextBlock />
</ScrollViewer>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WindowDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnActivate(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Activate\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnClosed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
//(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Closed\n";
//((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Closing");
//(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Closing\n";
//((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnContentRendered(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "ContentRendered\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnDeactivated(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Deactivated\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnLocationChanged(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "LocationChanged\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnStateChanged(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "StateChanged\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
}
}
C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WindowDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ScrollViewer scrollViewer = new ScrollViewer();
TextBlock textBlock = new TextBlock();
scrollViewer.Content = textBlock;
this.Content = scrollViewer;
this.Title = "MainWindow";
this.Width = 400;
this.Height = 450;
this.AllowsTransparency = false;
this.Icon = BitmapFrame.Create(new Uri("pack://application:,,,/Img/icon.png", UriKind.RelativeOrAbsolute));
this.Left = 0; this.Top = 0;
this.ResizeMode = ResizeMode.CanResizeWithGrip;
this.ShowActivated = true;
this.ShowInTaskbar = true;
this.SizeToContent = SizeToContent.Manual;
this.Topmost = false;
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.WindowState = WindowState.Normal;
this.Activated += OnActivate;
this.Closed += OnClosed;
this.Closing += OnClosing;
this.ContentRendered += OnContentRendered;
this.Deactivated += OnDeactivated;
this.LocationChanged += OnLocationChanged;
this.StateChanged += OnStateChanged;
}
private void OnActivate(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Activate\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnClosed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
//(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Closed\n";
//((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Closing");
//(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Closing\n";
//((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnContentRendered(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "ContentRendered\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnDeactivated(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "Deactivated\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnLocationChanged(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "LocationChanged\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
private void OnStateChanged(object sender, EventArgs e)
{
(((sender as Window).Content as ScrollViewer).Content as TextBlock).Text += "StateChanged\n";
((sender as Window).Content as ScrollViewer).ScrollToEnd();
}
}
}