В Silverlight XAML анализируется во время выполнения.
Из-за этого создание объектов, определенных в XAML, происходит медленнее, чем объектов, определенных в коде.
Я написал очень простой тест, и в моем тесте создание экземпляров объектов через скомпилированный код примерно в 4 раза быстрее, чем анализ Xaml.
Файл .g.cs в Silverlight используется для объявления полей, определенных в XAML, с использованием атрибута x: Name.
Он также используется для вызова синтаксического анализатора XAML и заполнения полей после этого.
Пожалуйста, посмотрите пример .g.cs в конце ответа.
Следуйте коду для моего очень упрощенного теста:
MainPage:
<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>
Соответствующий код 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();
}
}
}
Очень минимальный контроль в 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>
И код позади:
using System.Windows.Controls;
namespace SilverlightXamlVsCode
{
public partial class SilverlightControl1 : Button
{
public SilverlightControl1()
{
InitializeComponent();
}
}
}
====
Ниже приведен пример .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")));
}
}
}