WPF基础五:UI②内容元素Window

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 发生更改时使用的 RoutedEventpublic 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 的 Windowget; 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手动关闭 Windowpublic
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();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值