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

WPF应用程序顶级标签一定是Window吗?的更多相关文章

  1. wpf应用程序 打印标签

    新建一个wpf应用程序,Xaml如下: <Window x:Class="CreateBarCodeDemo.MainWindow" xmlns="http://s ...

  2. 【C#】1.3 WPF应用程序学习要点

    分类:C#.VS2015 创建日期:2016-06-14 使用教材:十二五国家级规划教材<C#程序设计及应用教程>(第3版) 一.要点概述 <C#程序设计及应用教程>(第3版) ...

  3. WPF 之 WPF应用程序事件

    当新建一个wpf应用程序,会自动生成一个App.xaml和MainWindow.xaml文件. 其中 App.xam 用来设置Application,应用程序的起始文件和资源及应用程序的一些属性和事件 ...

  4. 11、创建不使用XAML的WPF应用程序

    首先新建一个空的项目,然后添加一个类,引用一下程序集: PresentationCore.dll PresentationFramework.dll WindowsBase.dll namespace ...

  5. 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

    原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  6. 使用MVVM设计模式构建WPF应用程序

    使用MVVM设计模式构建WPF应用程序 本文是翻译大牛Josh Smith的文章,WPF Apps With The Model-View-ViewModel Design Pattern,译者水平有 ...

  7. 运用Edraw为WPF应用程序嵌入Office文档的方法总结

    具体描述了运用Edraw Office Viewer Component为WPF应用长须嵌入MS Word,Excel以及Power Point的方法. 打开Visual Studio,并创建一个新的 ...

  8. 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

    原文:写一个去除AI2XAML注释及多余数字位数的WPF窗体程序 使用正则表达式去除多余注释及冗余数字位,关键代码:            string pattern = @"/b(/d+ ...

  9. WPF应用程序内嵌网页

    原文:WPF应用程序内嵌网页 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/shaynerain/article/details/78160984 WPF ...

随机推荐

  1. [AngularFire 2] Joins in Firebase

    Lets see how to query Firebase. First thing, when we do query, 'index' will always help, for both SQ ...

  2. 安装hadoop2.6.0伪分布式环境 分类: A1_HADOOP 2015-04-27 18:59 409人阅读 评论(0) 收藏

    集群环境搭建请见:http://blog.csdn.net/jediael_lu/article/details/45145767 一.环境准备 1.安装linux.jdk 2.下载hadoop2.6 ...

  3. 分析Net 内存对象

    .Net 内存对象分析   在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的 ...

  4. stm32的ADC规则组通道采样顺序设置

    先看一下固件库手册 再看一下手册上的例子:  有两个通道,,并且顺序如下

  5. angular项目国际化配置(ngx-translate)

    原文 https://www.jianshu.com/p/7d1da3098625 大纲 1.认识ngx-translate 2.ngx-translate的配置步骤 3.ngx-translate的 ...

  6. 终端中经常使用的shell 命令

    Mac 在shell命令终端中,Ctrl+n相当于方向向下的方向键,Ctrl+p相当于方向向上的方向键. 在命令终端中通过它们或者方向键能够实现对历史命令的高速查找.这也是高速输入命令的技巧. 在命令 ...

  7. jquery-10 jquery中的绑定事件和解绑事件的方法是什么

    jquery-10 jquery中的绑定事件和解绑事件的方法是什么 一.总结 一句话总结:bind(); unbind(); one(); 1. jquery中的绑定事件和解绑事件的方法是什么? bi ...

  8. teamview huawei

    https://apkpure.com/add-on-huawei/com.teamviewer.quicksupport.addon.huawei   4.0/5 ( 5 Discussions ) ...

  9. Git使用经验小结

    2012春,开始正式使用SVN,2014年9月加入一起好后,开始使用Git.  有了一些使用经验,也看了下网上关于"Git全胜SVN"的言论. 结合自己的实际情况,简要写几句: 1 ...

  10. Ntp配置文件详解

    1. http://www.ine.com/resources/ntp-authentication.htm 2. http://blog.chinaunix.net/uid-773723-id-16 ...