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

来源:互联网 发布:爱奇艺mac历史版本 编辑:程序博客网 时间:2024/06/10 20:02

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,并比较了显示效果的差异。

原创粉丝点击