WPF应用程序顶级标签一定是Window吗?

简介: 原文:WPF应用程序顶级标签一定是Window吗? WPF应用程序顶级标签一定是Window吗? 很多人误以为是。可是,答案却是否定的。
原文: WPF应用程序顶级标签一定是Window吗?

WPF应用程序顶级标签一定是Window吗? 很多人误以为是。可是,答案却是否定的。
我们不妨来测试一下。

首先使用顶级标签为Window,这是最普通、也是最常见的情况。
新建一个WPF应用程序,名称为Window1,利用工具箱在窗口中拖入一个按钮(Button)。
我们发现Window1中将得到类似如下内容:
// Window1.xaml
<Window x:Class="LogicalOverrideApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
    <Button Height="23" Margin="96,33,107,0" Name="button1" VerticalAlignment="Top">Button</Button>
    </Grid>
</Window>

// Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace LogicalOverrideApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

按F5运行它,将得到如下运行结果:
常见的WPF窗体运行效果
(图1)

好了,下面我们来学学孙悟空的七十二变,变点花样出来看看:
1、首先,我们试试将Grid标签去掉,Window1.xaml变成:
// Window1.xaml
<Window x:Class="LogicalOverrideApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Button Height="23" Margin="96,33,107,0" Name="button1" VerticalAlignment="Top">Button</Button>
</Window>
按F5运行,效果一样。因为Grid只是窗体中内容的一个容器,在这里没有发挥出表格排列设计的效果,所以去掉之后是一样的。

2、再试试将顶级标签Window改成Page, Window1.xaml内容变成:
// Window1.xaml
<Page x:Class="LogicalOverrideApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Button Height="23" Margin="96,33,107,0" Name="button1" VerticalAlignment="Top">Button</Button>
</Page>
由于将顶级标签改成了Page,所以C#代码也得改成从Page继承:
// Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace LogicalOverrideApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Page
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}
按F5运行,效果图变成:
顶级标签换成Page后的效果图
(图2)

仔细看看图2与图1,有何差别?

首先我们发现窗体尺寸变了,不再是高宽均为300像素(可能不同的显示器会有所差异)。
其次,我们发现窗体的标题为空白,而且多了导航条。似乎Page的Title属性未记任何作用。如下图:

Window顶级标签改为Page后的差异图

(图3)

3、再试试改成其他的标签,比如Canvas。由于Canvas没有Title属性,所以,要将Title属性去掉。
<Canvas x:Class="LogicalOverrideApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Button Height="23" Margin="96,33,107,0" Name="button1" VerticalAlignment="Top">Button</Button>
</Canvas>
同理,C#代码也需要将父类改成Canvas(其他代码从略):
...
public partial class Window1 : Canvas
...

按F5运行结果,与图2无差异。
你还可以试试改为StackPanel等。不再赘述,慢慢分析一下,定有收获。

小结:本篇通过将Window标签改为Page,Canvas,StackPanel等,说明了WPF中窗体的顶级标签不一定是Window,并比较了显示效果的差异。

目录
相关文章
|
前端开发 Ubuntu Linux
【.NET6+Avalonia】开发支持跨平台的仿WPF应用程序以及基于ubuntu系统的演示
随着跨平台越来越流行,.net core支持跨平台至今也有好几年的光景了。但是目前基于.net的跨平台,大多数还是在使用B/S架构的跨平台上;至于C/S架构,大部分人可能会选择QT进行开发,或者很早之前还有一款Mono可以支持.NET开发者进行开发跨平台应用。
835 0
【.NET6+Avalonia】开发支持跨平台的仿WPF应用程序以及基于ubuntu系统的演示
|
C#
WPF 4 Ribbon 开发 之 应用程序菜单(Application Menu)
原文:WPF 4 Ribbon 开发 之 应用程序菜单(Application Menu)      在上一篇中我们完成了快捷工具栏的开发,本篇将讲解应用程序菜单开发的相关内容。如下图所示,点击程序窗口左上角的记事本图标(Application Button)会显示出应用程序菜单(Application Menu)列表,列表中的按键即为软件的一些基本功能。
2110 0
|
7月前
|
C#
C#开源的虚拟桌宠模拟器,可以内置到任何WPF应用程序 - VPet
C#开源的虚拟桌宠模拟器,可以内置到任何WPF应用程序 - VPet
|
4月前
|
数据可视化 API C#
|
6月前
|
人工智能 C#
WPF自定义控件库之Window窗口
本文以自定义窗口为例,简述WPF开发中如何通过自定义控件来扩展功能和样式,仅供学习分享使用,如有不足之处,还请指正。
151 5
|
6月前
|
人工智能 C#
虚拟桌宠模拟器:VPet-Simulator,一个开源的桌宠软件, 可以内置到任何WPF应用程序
虚拟桌宠模拟器:VPet-Simulator,一个开源的桌宠软件, 可以内置到任何WPF应用程序
虚拟桌宠模拟器:VPet-Simulator,一个开源的桌宠软件, 可以内置到任何WPF应用程序
|
8月前
|
C#
WPF技术之Xaml Window
WPF Window 是一个 WPF 窗口类,它具有许多属性枚举可以控制窗口的外观和行为。
76 0
WPF技术之Xaml Window
|
11月前
|
C#
4.使用代码和未经编译的XAML创建WPF应用程序
4.使用代码和未经编译的XAML创建WPF应用程序
58 0
|
11月前
|
C# C++ Windows
3.只使用代码创建WPF应用程序
3.只使用代码创建WPF应用程序
81 0