Xaml技术:浅析为什么说一个标签就是new一个对象?
2014-08-08 20:29
441 查看
在Xaml技术中,如WPF、Silverlight等,一个标签就是一个对象。很多人不理解为什么,现在我们就来一起研究一下吧!
首先,我们新建一个WPF应用程序,并在窗口中添加一个按钮:
![](http://img.blog.csdn.net/20140808205041898?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE3MDk2Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
调试运行后,打开文件夹...\WpfApplication1\obj\x86\Debug,我们会发现MainWindow.g.cs和MainWindow.g.i.cs两个文件,这是干什么的呢?我们打开看看:
![](http://img.blog.csdn.net/20140808204846453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE3MDk2Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
注意:添加的控件需要唯一ID,也就是Name属性,否则在这两个文件中将找不到该对象的定义与初始化
首先,我们新建一个WPF应用程序,并在窗口中添加一个按钮:
<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两个文件,这是干什么的呢?我们打开看看:
#################################<strong><span style="color:#ff0000;">MainWindow.g.cs</span></strong>#############################################
#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; } } }
<pre name="code" class="csharp">#################################<strong><span style="color: rgb(255, 0, 0);">MainWindow.g.i.cs</span></strong>#############################################
#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属性,否则在这两个文件中将找不到该对象的定义与初始化
相关文章推荐
- bean标签,就是生成一个对象
- 为什么很多人都喜欢new一个类,而不是直接声明一个对象呢?
- [企业管理]如何与一个纯技术的老板合作第五篇——当初我为什么离开电信还会选择进入中科院
- 一个利用了异步处理,事件驱动,对象序列化等技术的类Parser完整代码示例
- (原创)利用扩展方法,给 IEnumerable<T> 增加一个生成 Html 的 select 标签的方法,不用 C# 中的反射技术
- 一个数据分析系统的技术架构设计浅析
- WPF架构关键技术剖析(2)--XAML、对象序列化、类动态实例化
- 浅析SQL SERVER一个没有公开的存储过程 (更改对象的所有者)
- 为什么我觉得MySQL就是一个玩具
- 为什么新建文本文档中输入“联通”保存,在打开时就是一个废电池的样子?
- 技术就是一个大大的迷宫
- 为什么要在addsubview:一个view对象后,release它
- 在一个类内不可以定义自身类的对象,为什么定义自身类的静态对象又是正确的
- [JavaScript]每个函数就是一个对象(一):申明函数的三种方法
- JavaScript 申明函数的三种方法 每个函数就是一个对象(一)
- 写一个方法进行各种属性的更新,而不要每个界面设置改变都改变一个属性。就是点击应用或确定按钮时调用这个更新属性的方法,遍历所有(控制对象属性的)界面控件的状态(值),进行属性修改和刷新。
- 如何把一个用户下面的表和其他对象,一次性导入到另一个用户中去???也就是拷贝一份到另一个用户中。
- 一个jstl标签综合运用的例子(包括下拉列表事件处理等等),直观了解一个浏览器页面上有多少对象)
- Trunk技术的一点应用,一个对象可以聚合多个Timer对象的实现
- (原创)利用扩展方法,给 IEnumerable<T> 增加一个生成 Html 的 CheckBox 标签的方法,不用 C# 中的反射技术