户通过窗口与 Windows Presentation Foundation (WPF) 独立应用程序进行交互。Window 类来提供它们自己的窗口

窗口类

窗口的构成部分。
窗口分为两个区域:非工作区和工作区。

窗口的非工作区由 WPF 实现,它包括大多数窗口所共有的窗口部分,其中包括:

  • 边框。

  • 标题栏。

  • 图标。

  • “最小化”、“最大化”和“还原”按钮。

  • “关闭”按钮。

  • “系统”菜单,其中包含允许用户最小化、最大化、还原、移动和关闭窗口以及调整窗口大小的菜单项。

窗口的工作区是窗口的非工作区内部的区域,开发人员使用它来添加应用程序特定的内容,如菜单栏、工具栏和控件。

Window 类进行封装,使用该类可以执行下面的操作:

  • 显示窗口。

  • 配置窗口的大小、位置和外观。

  • 承载应用程序特定的内容。

  • 管理窗口的生存期。

 
窗口生存期

和所有类一样,窗口也有生存期,在第一次实例化窗口时生存期开始,然后就可以打开、激活和停用窗口,直到最终关闭窗口。

本节包含下列子节。

打开窗口

若要打开窗口,首先应创建一个窗口实例,下面的示例演示此操作。

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.App"
    Startup="app_Startup">
</Application>
 
using System.Windows;
namespace SDKSample
{
    public partial class App : Application
    {
        void app_Startup(object sender, StartupEventArgs e)
        {
            // Create a window
            MarkupAndCodeBehindWindow window = new MarkupAndCodeBehindWindow();

            // Open a window
            window.Show();
        }
    }
}

MarkupAndCodeBehindWindow 进行实例化。

Application.MainWindow)。

Show 方法打开窗口;结果如下图所示。

Show 打开的窗口是无模式窗口,这意味着应用程序所运行的模式允许用户在同一个应用程序中激活其他窗口。

SourceInitialized 事件并显示窗口。

StartupUri 以指定在应用程序启动时自动打开的第一个窗口。

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.App"
    StartupUri="PlainWindow.xaml" />

Show 方法来打开窗口。

窗口所属权

Show 方法打开的窗口与创建它的窗口之间没有隐式关系;用户可以与这两个窗口分别进行独立的交互,这意味着这两个窗口都可以执行下面的操作:

  • true)。

  • 在不影响另一个窗口的情况下,最小化、最大化和还原一个窗口。

这将在下面的示例中显示。

// Create a window and make this window its owner
Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.Show();

建立了所属权之后:

  • Owner 属性的值来引用它的所有者窗口。

  • OwnedWindows 属性的值来发现它拥有的全部窗口。

防止窗口激活

有些情况下,不应在显示窗口时将其激活,例如 Internet Messenger 风格的应用程序的对话窗口或电子邮件应用程序的通知窗口。

这样:

  • 窗口便不会被激活。

  • Activated 事件。

  • 当前激活的窗口保持激活状态。

在这种情况下:

  • 窗口被激活。

  • Activated 事件。

  • 停用以前激活的窗口。

  • Activated 事件。

窗口激活

Activated 事件。

Activated

Deactivated 以实现此行为。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.CustomMediaPlayerWindow"
    Activated="window_Activated"
    Deactivated="window_Deactivated">

    <!-- Media Player -->
    <MediaElement 
      Name="mediaElement" 
      Stretch="Fill" 
      LoadedBehavior="Manual" 
      Source="numbers.wmv" />

</Window>
 
C#
using System; // EventArgs
using System.Windows; // Window

namespace SDKSample
{
    public partial class CustomMediaPlayerWindow : Window
    {
        public CustomMediaPlayerWindow()
        {
            InitializeComponent();
        }

        void window_Activated(object sender, EventArgs e)
        {
            // Recommence playing media if window is activated
            this.mediaElement.Play();
        }

        void window_Deactivated(object sender, EventArgs e)
        {
            // Pause playing if media is being played and window is deactivated
            this.mediaElement.Pause();
        }
    }
}

关闭窗口

可以使用非工作区中的元素来关闭窗口,这些元素包括:

  • “关闭”项。

  • 按 Alt + F4。

  • “关闭”按钮。

可以为工作区提供关闭窗口的附加机制,其中比较常用的包括:

  • “退出”项,通常用于主应用程序窗口。

  • “关闭”项,通常出现在辅助应用程序窗口中。

  • “取消”按钮,通常出现在模式对话框中。

  • “关闭”按钮,通常出现在无模式对话框中。

“退出”来关闭窗口的功能。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.WindowWithFileExit">

  <Menu>
    <MenuItem Header="_File">
      <MenuItem Header="E_xit" Click="fileExitMenuItem_Click" />
    </MenuItem>
  </Menu>

</Window>
 
using System.Windows; // window, RoutedEventArgs

namespace SDKSample
{
    public partial class WindowWithFileExit : System.Windows.Window
    {
        public WindowWithFileExit()
        {
            InitializeComponent();
        }

        void fileExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            // Close this window
            this.Close();
        }
    }
}


Closed

Closing 的关键方面

 

窗口生存期事件

下面的插图显示了窗口的生存期中的主体事件的顺序。

false)。

可以设置这些属性以更改窗口的位置。

Window 第一次出现时的初始位置:

Window 将向 Windows 请求显示的位置。

最顶层窗口和 Z 顺序

true 可以使窗口位于最顶层 z 顺序中。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Topmost="True">


...


</Window>

在每个 z 顺序中,当前活动的窗口都会显示在同一 z 顺序中的所有其他窗口之上。

SizeToContent

MaxWidth 用于管理窗口在生存期中可以具有的宽度的范围,其配置如下面的示例所示。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    MinWidth="300" Width="400" MaxWidth="500">


...


</Window>

MaxHeight 管理,其设置如下面的示例所示。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    MinHeight="300" Height="400" MaxHeight="500">


...


</Window>

ActualHeight

SizeToContent 属性,该属性具有下面的值:

  • 不起任何作用(默认)。

  • MaxWidth 设置为内容的宽度具有相同的效果。

  • MaxHeight 设置为内容的高度具有相同的效果。

  • MaxWidth 设置为内容的宽度具有相同的效果。

下面的示例演示一个窗口自动调整大小垂直和水平地适应其内容,,那么,当首次显示。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    SizeToContent="WidthAndHeight">


...


</Window>


SizeToContent 属性指定调整窗口大小以适合其内容。

// Manually alter window height and width
this.SizeToContent = SizeToContent.Manual;

// Automatically resize width relative to content
this.SizeToContent = SizeToContent.Width;

// Automatically resize height relative to content
this.SizeToContent = SizeToContent.Height;

// Automatically resize height and width relative to content
this.SizeToContent = SizeToContent.WidthAndHeight;

Window 会使用下面的优先级顺序来计算大小属性的值。

对于高度属性:

  1. FrameworkElement.MinHeight >

  2. FrameworkElement.MaxHeight >

  3. SizeToContent.WidthAndHeight >

  4. FrameworkElement.Height

对于宽度属性:

  1. FrameworkElement.MinWidth >

  2. FrameworkElement.MaxWidth >

  3. SizeToContent.WidthAndHeight >

  4. FrameworkElement.Width

WindowState 属性管理。

处于此状态的窗口如果是可调整大小的,则允许用户使用大小调整手柄或边框来移动窗口或调整窗口大小。

尽管不在任务栏中显示的最小化窗口可以在桌面上四处拖动,但是不能使用边框或大小调整手柄来调整上述两种最小化窗口的大小。

与最小化窗口一样,最大化窗口的大小也无法通过使用大小调整手柄或拖动边框来调整。

WindowState 枚举值之一:

下面的示例说明如何创建在打开时最大化显示的窗口。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    WindowState="Maximized">


...


</Window>


一旦显示了可调整大小的窗口,用户便可以按窗口的标题栏上的最小化、最大化和还原按钮来更改窗口状态。

Title

还可以通过配置窗口的大小调整模式、窗口样式,以及窗口是否显示为桌面任务栏中的按钮,来更改非工作区边框的外观和行为。

 

本节包含下列子节。

大小调整模式

“调整大小”按钮,以及如果显示这些按钮是否启用它们。

ResizeMode 枚举值之一:

WindowStyle 相同,窗口的大小调整模式在其生存期内不能更改,这意味着您很可能会通过 XAML 标记设置该模式。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    ResizeMode="CanResizeWithGrip">


...


</Window>

 

窗口样式

但是,在有些情况下,可能需要使用不同类型的边框,或根本不需要边框,这要取决于窗口的类型。

WindowStyle 枚举值之一:

下图显示了这些窗口样式的效果。

WindowStyle;因为窗口样式不能在窗口的生存期内更改,所以您很可能会使用 XAML 标记对其进行配置。
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    WindowStyle="ToolWindow">


...


</Window>


非矩形窗口样式

例如,您可能希望创建一个带有非矩形边框的应用程序,如 Microsoft Windows Media Player 所使用的边框。

下图中显示的对话气泡框就是一个例子。

Window 对透明度的特殊支持,可以创建此类型的窗口。
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="Transparent">


...


</Window>

 

相关文章:

  • 2022-01-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-24
  • 2021-10-27
相关资源
相似解决方案