原文: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. [TypeScript] Interface and Class

    When to use Interface and when to use Class. Let's see one example: export interface Lesson { course ...

  2. [CSS] Target empty elements using the :empty pseudo-class

    You can target an element that has no child elements by using the :empty pseudo-class. With browser ...

  3. XML Parser Errors See Details for more Information XML Parser Error on line 1: Document root ele

    1.错误描写叙述 XML Parser Errors See Details for more Information XML Parser Error on line 1: Document roo ...

  4. Fragment的基本应用

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/50933621 Fragment 还是先来基本介绍. Fragment –> 片段 ...

  5. [CSS] Control Image Aspect Ratio Using CSS

    Resize images and videos to fill their parent and maintain their aspect ratio with pure CSS. The new ...

  6. 无意中发现Markdown,最终解放了我

    文件夹 概述 换行 删除线 链接自己主动识别 表格 代码块高亮 定义列表 脚注 自己主动生成文件夹 參考资料 正文 概述 大部分情况下,Markdown的基本的语法已够我们使用,比方随性记录点东西.非 ...

  7. 分类算法简介 分类: B10_计算机基础 2015-03-09 11:08 257人阅读 评论(0) 收藏

    一.决策树 决策树是用于分类和预测的主要技术之一,决策树学习是以实例为基础的归纳学习算法,它着眼于从一组无次序.无规则的实例中 推理出以决策树表示的分类规则.构造决策树的目的是找出属性和类别间的关系, ...

  8. 一个自己主动依据xcode中的objective-c代码生成类关系图的神器

    https://github.com/kimsungwhee/KSHObjcUML 安装方法: 1.下载项目 2.执行 3.会又一次开启一个新的xcode 4.选择一个项目,点击 Objc-UML 会 ...

  9. 前端js实现打印(导出)excel表格

    产品原型: 图片.png 功能需求:点击导出考勤表格按钮,会自动下载成Excel格式 图片.png 图片.png jsp页面代码: <div class="tools"> ...

  10. Django表单上传

    任务描述:实现表单提交(上传文件) 1.项目目录: 2.源代码: regist.html <!DOCTYPE html> <html lang="en"> ...