Xaml技术:浅析为什么说一个标签就是new一个

如题所述

第1个回答  2018-02-22

首先,我们新建一个WPF应用程序,并在窗口中添加一个按钮:

[csharp] view plain copy

    <Window x:Class="WpfApplication1.MainWindow"  

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  

    Title="MainWindow" Height="350" Width="525">  

    <Grid>  

    <span style="color:#ff0000;"><strong><Button Name="btnHaHa" Content="哈哈" Foreground="Red" Margin="118,64,127,141"></Button></strong></span>  

    </Grid>  

    </Window>  

    调试运行后,打开文件夹...\WpfApplication1\obj\x86\Debug,我们会发现MainWindow.g.cs和MainWindow.g.i.cs两个文件,这是干什么的呢?我们打开看看:


    [csharp] view plain copy

    #################################<strong><span style="color:#ff0000;">MainWindow.g.cs</span></strong>#############################################  

    [csharp] view plain copy

    #pragma checksum "..\..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "93AC7AD7A7AD9977990F9BD74B70C70D"  

    //------------------------------------------------------------------------------  

    // <auto-generated>  

    //     此代码由工具生成。  

    //     运行时版本:4.0.30319.18444  

    //  

    //     对此文件的更改可能会导致不正确的行为,并且如果  

    //     重新生成代码,这些更改将会丢失。  

    // </auto-generated>  

    //------------------------------------------------------------------------------  

    using System;  

    using System.Diagnostics;  

    using System.Windows;  

    using System.Windows.Automation;  

    using System.Windows.Controls;  

    using System.Windows.Controls.Primitives;  

    using System.Windows.Data;  

    using System.Windows.Documents;  

    using System.Windows.Ink;  

    using System.Windows.Input;  

    using System.Windows.Markup;  

    using System.Windows.Media;  

    using System.Windows.Media.Animation;  

    using System.Windows.Media.Effects;  

    using System.Windows.Media.Imaging;  

    using System.Windows.Media.Media3D;  

    using System.Windows.Media.TextFormatting;  

    using System.Windows.Navigation;  

    using System.Windows.Shapes;  

    using System.Windows.Shell;  

    namespace WpfApplication1 {  

    /// <summary>  

    /// MainWindow  

    /// </summary>  

    public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {  

    #line 6 "..\..\..\MainWindow.xaml"  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]  

    <span style="color:#ff0000;"><strong>internal System.Windows.Controls.Button btnHaHa;</strong></span>  

    #line default  

    #line hidden  

    private bool _contentLoaded;  

    /// <summary>  

    /// InitializeComponent  

    /// </summary>  

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]  

    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]  

    public void <span style="color:#000066;"><strong>InitializeComponent</strong></span>() {  

    if (_contentLoaded) {  

    return;  

    }  

    _contentLoaded = true;  

    System.Uri resourceLocater = new System.Uri("/WpfApplication1;component/mainwindow.xaml", System.UriKind.Relative);  

    #line 1 "..\..\..\MainWindow.xaml"  

    System.Windows.Application.LoadComponent(this, resourceLocater);  

    #line default  

    #line hidden  

    }  

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]  

    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]  

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]  

    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {  

    switch (connectionId)  

    {  

    case 1:  

    <span style="color:#ff0000;background-color: rgb(255, 255, 255);"><strong>this.btnHaHa = ((System.Windows.Controls.Button)(target));</strong></span>  

    return;  

    }  

    this._contentLoaded = true;  

    }  

    }  

    }  

    [csharp] view plain copy

    <pre name="code" class="csharp">#################################<strong><span style="color: rgb(255, 0, 0);">MainWindow.g.i.cs</span></strong>#############################################  


    [csharp] view plain copy

    #pragma checksum "..\..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "93AC7AD7A7AD9977990F9BD74B70C70D"  

    //------------------------------------------------------------------------------  

    // <auto-generated>  

    //     此代码由工具生成。  

    //     运行时版本:4.0.30319.18444  

    //  

    //     对此文件的更改可能会导致不正确的行为,并且如果  

    //     重新生成代码,这些更改将会丢失。  

    // </auto-generated>  

    //------------------------------------------------------------------------------  

    using System;  

    using System.Diagnostics;  

    using System.Windows;  

    using System.Windows.Automation;  

    using System.Windows.Controls;  

    using System.Windows.Controls.Primitives;  

    using System.Windows.Data;  

    using System.Windows.Documents;  

    using System.Windows.Ink;  

    using System.Windows.Input;  

    using System.Windows.Markup;  

    using System.Windows.Media;  

    using System.Windows.Media.Animation;  

    using System.Windows.Media.Effects;  

    using System.Windows.Media.Imaging;  

    using System.Windows.Media.Media3D;  

    using System.Windows.Media.TextFormatting;  

    using System.Windows.Navigation;  

    using System.Windows.Shapes;  

    using System.Windows.Shell;  

    namespace WpfApplication1 {  

    /// <summary>  

    /// MainWindow  

    /// </summary>  

    public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {  

    #line 6 "..\..\..\MainWindow.xaml"  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]  

    <strong><span style="color:#ff0000;">internal System.Windows.Controls.Button btnHaHa;</span></strong>  

    #line default  

    #line hidden  

    private bool _contentLoaded;  

    /// <summary>  

    /// InitializeComponent  

    /// </summary>  

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]  

    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]  

    public void <span style="color:#000066;"><strong>InitializeComponent</strong></span>() {  

    if (_contentLoaded) {  

    return;  

    }  

    _contentLoaded = true;  

    System.Uri resourceLocater = new System.Uri("/WpfApplication1;component/mainwindow.xaml", System.UriKind.Relative);  

    #line 1 "..\..\..\MainWindow.xaml"  

    System.Windows.Application.LoadComponent(this, resourceLocater);  

    #line default  

    #line hidden  

    }  

    [System.Diagnostics.DebuggerNonUserCodeAttribute()]  

    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]  

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]  

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]  

    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {  

    switch (connectionId)  

    {  

    case 1:  

    <strong><span style="color:#ff0000;">this.btnHaHa = ((System.Windows.Controls.Button)(target));</span></strong>  

    return;  

    }  

    this._contentLoaded = true;  

    }  

    }  

    }  

    我们看到了什么?是不是很熟悉了,是不是和Winform中的窗体设计文件很类似呀!

    注意:添加的控件需要唯一ID,也就是Name属性,否则在这两个文件中将找不到该对象的定义与初始化

相似回答
大家正在搜