原文: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运行它,将得到如下运行结果:

(图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运行,效果图变成:

(图2)

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

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



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

最新文章

  1. Nginx支持比Apache高并发的原因
  2. 外网访问内网工具ngrok tunnel 使用总结
  3. 第一章 Shiro简介——《跟我学Shiro》(转)
  4. jquery中focus()失效怎么解决
  5. __block和__weak的区别
  6. 转:SVN服务器搭建和使用(三)
  7. 【转】15个无比华丽的HTML5/CSS3动画应用
  8. 给你完美浪漫的七夕,APICloud送你双人电影票!
  9. 20145207《Java程序设计》第9周学习总结
  10. [SAP ABAP开发技术总结]列表屏幕
  11. LNMP一键安装包 V1.1 通告
  12. 【转】在 2016 年做 PHP 开发是一种什么样的体验?(一)
  13. Linux文件管理浅析(一) _磁盘管理基础
  14. SpringBoot 分布式session
  15. 561. Array Partition I
  16. Tengine 安装和说明
  17. go标准库的学习-runtime
  18. Quartz应用与集群原理分析
  19. day25 上山练习 计算圆练习
  20. Postman使用记录

热门文章

  1. [array] leetCode-18. 4Sum -Medium
  2. 利用a标签导出csv文件
  3. (三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1
  4. [CSS] Target Positional Elements Using *-Of-Type CSS pseudo-classes
  5. 【solr基础教程之九】客户端 分类: H4_SOLR/LUCENCE 2014-07-30 15:28 904人阅读 评论(0) 收藏
  6. 因权限引起的svn提交失败的错误及其解决办法
  7. css 翻牌 翻转 3d翻转 特效
  8. Android Studio Gradle:Resolvedependencies&#39;:app:_debugCompile&#39; 问题解决纪录
  9. SSL/TLS协议运行机制的概述(转)
  10. amazeui-js插件-ui增强-日期组件如何使用(把实例做一下)