从编译代码的纯粹速度角度来看,如果我使用100%XAML或100%代码隐藏或其间的某种组合,它会有所不同吗?代码隐藏中没有像if
声明一样的逻辑- 它与XAML将加载内容元素的操作完全相同.
如果它确实有所作为,我怎么能测试它在速度方面有多大差异?
在Silverlight中,XAML在运行时进行解析.
因为在XAML中定义的对象的实例化比在代码中定义的对象慢. 我写了一个非常简单的测试,在我的测试实例化对象中,通过编译代码比解析Xaml快4倍.Silverlight中的.g.cs文件用于使用x:Name属性声明在XAML中定义的字段.
它还用于调用XAML解析器并在此之后填充字段. 请在响应结束时查看.g.cs的示例.按照我非常简单的测试代码:
主页:<UserControl x:Class="SilverlightXamlVsCode.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Vertical">
<Button
Content="Start Xaml"
Width="150"
Height="27"
Click="OnStartXaml" />
<TextBlock
x:Name="_startTimeXaml" />
<TextBlock
x:Name="_endTimeXaml" />
<TextBlock
x:Name="_durationXaml" />
<ContentControl
x:Name="_content" />
<Button
Content="Start Code"
Width="150"
Height="27"
Click="OnStartCode" />
<TextBlock
x:Name="_startTimeCode" />
<TextBlock
x:Name="_endTimeCode" />
<TextBlock
x:Name="_durationCode" />
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
相应的MainPage代码背后:
using System;
using System.Windows;
using System.Windows.Controls;
namespace SilverlightXamlVsCode
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void OnStartCode(object sender, RoutedEventArgs e)
{
var startTime = DateTime.Now;
_startTimeCode.Text = startTime.ToString();
for (int i = 0; i < 10000; i++)
{
_content.Content = new Button() {Width = 100, Height = 27, Content = "Code"};
}
var endTime = DateTime.Now;
_endTimeCode.Text = endTime.ToString();
var duration = endTime - startTime;
_durationCode.Text = duration.TotalSeconds.ToString();
}
private void OnStartXaml(object sender, RoutedEventArgs e)
{
var startTime = DateTime.Now;
_startTimeXaml.Text = startTime.ToString();
for (int i = 0; i < 10000; i++)
{
_content.Content = new SilverlightControl1();
}
var endTime = DateTime.Now;
_endTimeXaml.Text = endTime.ToString();
var duration = endTime - startTime;
_durationXaml.Text = duration.TotalSeconds.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Xaml中的一个非常小的控件:
<Button x:Class="SilverlightXamlVsCode.SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="100" Height="27" Content="Xaml">
</Button>
Run Code Online (Sandbox Code Playgroud)
和代码背后:
using System.Windows.Controls;
namespace SilverlightXamlVsCode
{
public partial class SilverlightControl1 : Button
{
public SilverlightControl1()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
====
请参阅下面的.g.cs示例:#pragma checksum "C:\Workspace\Playplace\SilverlightXamlVsCode\SilverlightXamlVsCode\MainPage.xaml"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3603
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
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.Interop;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace SilverlightXamlVsCode
{
public partial class MainPage : System.Windows.Controls.UserControl {
internal System.Windows.Controls.TextBlock _startTimeXaml;
internal System.Windows.Controls.TextBlock _endTimeXaml;
internal System.Windows.Controls.TextBlock _durationXaml;
internal System.Windows.Controls.ContentControl _content;
internal System.Windows.Controls.TextBlock _startTimeCode;
internal System.Windows.Controls.TextBlock _endTimeCode;
internal System.Windows.Controls.TextBlock _durationCode;
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
// INVOKE XAML PARSER HERE
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightXamlVsCode;component/MainPage.xaml", System.UriKind.Relative));
this._startTimeXaml = ((System.Windows.Controls.TextBlock)(this.FindName("_startTimeXaml")));
this._endTimeXaml = ((System.Windows.Controls.TextBlock)(this.FindName("_endTimeXaml")));
this._durationXaml = ((System.Windows.Controls.TextBlock)(this.FindName("_durationXaml")));
this._content = ((System.Windows.Controls.ContentControl)(this.FindName("_content")));
this._startTimeCode = ((System.Windows.Controls.TextBlock)(this.FindName("_startTimeCode")));
this._endTimeCode = ((System.Windows.Controls.TextBlock)(this.FindName("_endTimeCode")));
this._durationCode = ((System.Windows.Controls.TextBlock)(this.FindName("_durationCode")));
}
}
}
Run Code Online (Sandbox Code Playgroud)