Silverlight XAML与代码隐藏

Silverlight XAML与代码隐藏,silverlight,xaml,Silverlight,Xaml,从编译代码的纯速度角度来看,如果我使用100%XAML或100%代码隐藏或两者之间的某种组合,会有什么不同吗?代码中没有像if语句那样的逻辑——它的作用与XAML加载内容元素的作用完全相同 如果它确实起到了作用,我如何测试它在速度方面有多大的不同呢?您不太可能看到两者之间有多大的性能差异,因为XAML最终被翻译成代码并编译。如果将所有UI放在纯代码中,您将失去XAML作为以UI为中心的领域特定语言所提供的好处。重要的是要保持关注点的分离,保持UI布局和样式与驱动UI的代码分离,而且,保持这两者与

从编译代码的纯速度角度来看,如果我使用100%XAML或100%代码隐藏或两者之间的某种组合,会有什么不同吗?代码中没有像if语句那样的逻辑——它的作用与XAML加载内容元素的作用完全相同


如果它确实起到了作用,我如何测试它在速度方面有多大的不同呢?

您不太可能看到两者之间有多大的性能差异,因为XAML最终被翻译成代码并编译。如果将所有UI放在纯代码中,您将失去XAML作为以UI为中心的领域特定语言所提供的好处。重要的是要保持关注点的分离,保持UI布局和样式与驱动UI的代码分离,而且,保持这两者与业务逻辑分离,可以生成更易于维护的应用程序

XAML是一个优秀的UI构建工具…比纯代码更适合此工作。我建议将UI布局和样式保持在XAML形式

如果您想进一步了解XAML是如何构建的,我建议您阅读以下内容:


使用WPF,编译XAML后会生成两个文件:一个是表示UI行为的.g.cs文件,另一个是表示XAML节点层次结构的紧凑二进制文件.baml文件。使用Silverlight,可以在.g.cs或.g.vb文件中获得生成的代码,但不能获得BAML。虽然最终的结果是编译任何行为,但使用Silverlight,您仍然只能使用包含UI节点层次结构的XML文件,而不是更紧凑的二进制表示。由于Silverlight browser控件的局限性,或者可能是它所使用的有限的.NET framework,不太清楚为什么会出现这种情况。

您不太可能看到两者之间有多大的性能差异,因为XAML最终会被翻译成代码并编译。如果将所有UI放在纯代码中,您将失去XAML作为以UI为中心的领域特定语言所提供的好处。重要的是要保持关注点的分离,保持UI布局和样式与驱动UI的代码分离,而且,保持这两者与业务逻辑分离,可以生成更易于维护的应用程序

XAML是一个优秀的UI构建工具…比纯代码更适合此工作。我建议将UI布局和样式保持在XAML形式

如果您想进一步了解XAML是如何构建的,我建议您阅读以下内容:


使用WPF,编译XAML后会生成两个文件:一个是表示UI行为的.g.cs文件,另一个是表示XAML节点层次结构的紧凑二进制文件.baml文件。使用Silverlight,可以在.g.cs或.g.vb文件中获得生成的代码,但不能获得BAML。虽然最终的结果是编译任何行为,但使用Silverlight,您仍然只能使用包含UI节点层次结构的XML文件,而不是更紧凑的二进制表示。由于Silverlight浏览器控件的限制,或者可能是它所使用的有限的.NET框架,不太清楚这是什么原因。

在Silverlight XAML中,是在运行时解析的

因为XAML中定义的对象的实例化比代码中定义的对象慢

我编写了一个非常简单的测试,在我的测试中,通过编译代码实例化对象的速度大约是解析Xaml的4倍

Silverlight中的.g.cs文件用于声明XAML中使用x:Name属性定义的字段。

它还用于调用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>
====

请参见下面的.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")));
        }
    }
}

在Silverlight中,XAML是在运行时解析的

因为XAML中定义的对象的实例化比代码中定义的对象慢

我编写了一个非常简单的测试,在我的测试中,通过编译代码实例化对象的速度大约是解析Xaml的4倍

Silverlight中的.g.cs文件用于声明XAML中使用x:Name属性定义的字段。

它还用于调用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>
====

请参见下面的.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")));
        }
    }
}

我试图复制克林格答案的基准调查结果。我发现他的基准没有准确地测量时间,因为Silverlight在OnClick函数返回之前不会进行任何渲染,这意味着在计时器停止计数之前,模板解析或更复杂的XAML工作实际上都不会完成。我没有比这更好的时机了 此时提出的解决方案


实际标记为正式答案的答案仅适用于WPF。在Silverlight中,XAML不会转换为编译代码

我试图复制克林格答案的基准调查结果。我发现他的基准没有准确地测量时间,因为Silverlight在OnClick函数返回之前不会进行任何渲染,这意味着在计时器停止计数之前,模板解析或更复杂的XAML工作实际上都不会完成。我现在没有更好的时间解决方案


实际标记为正式答案的答案仅适用于WPF。在Silverlight中,XAML不会转换为编译代码

感谢您提供的详细信息,特别是关于.g.vb文件的信息-我根本不知道这一点。我正在用代码自动生成XAML,因此我可以同样轻松地生成后面的代码,所有这些都不容易!,这就是我问的原因。如果代码隐藏速度更快,那么我会这样做,但是如果XAML转换为代码隐藏,它可能会以一种高效的方式转换,因此我可能会坚持自动生成XAML。@Otaku:真的需要加载新组件吗?不要在代码中这样做。按照应该做的去做,然后看看你是否真的需要加快速度。对于许多Silverlight应用程序中经常使用的DataTemplate之类的东西,您打算怎么做。这些内容最初需要用Xaml定义。我真的不认为这是你需要担心的事情。事实上,如果是这样的话,你最好考虑密集使用数据模板。@AnthonyWJones:不确定密集的需求。我将在页面上添加10到100个项目,包括形状、路径、网格、文本框、自定义字体、图像等。每一个项目都可能进行多种操作,例如,不仅是一个矩形,还有一个带有着色器的矩形、imagebrush填充、imagebrush上的变换等。。我只是想知道什么可能是最快的。如果你是说基于我刚才描述的,XAML和代码隐藏是相同/相似的,那很酷,我只是不了解我自己。御宅族:最好先了解一下什么是真正有效的,然后让软件真正编写出来。然后考虑它是否足够快。说真的,你花在研究如何对抗系统上的时间,因为你没有以名义上的方式做事,这比发现你需要重新编写一些Xaml作为代码更有害,毕竟这是一个容易处理的转换练习。即使您预先知道它需要的是代码而不是Xaml,我仍然会先在Xaml中执行,让它工作,然后将Xaml位移植到代码中。顺便说一句,10-100项是微不足道的。@AnthonyWJones:洞察力很好,尤其是10-100项不重要。我有自动写入XAML的功能,它似乎运行得很好,我只是不确定我是否错过了一个大好机会。XAML是非常舒适的,它的代码隐藏在我的手中感觉不稳定,但我现在正在探索各种各样的选项,因为在某些情况下,例如,我仍然需要一些代码隐藏。感谢您提供的详细信息,特别是关于.g.vb文件-我根本不知道这一点。我正在用代码自动生成XAML,因此我可以同样轻松地生成后面的代码,所有这些都不容易!,这就是我问的原因。如果代码隐藏速度更快,那么我会这样做,但是如果XAML转换为代码隐藏,它可能会以一种高效的方式转换,因此我可能会坚持自动生成XAML。@Otaku:真的需要加载新组件吗?不要在代码中这样做。按照应该做的去做,然后看看你是否真的需要加快速度。对于许多Silverlight应用程序中经常使用的DataTemplate之类的东西,您打算怎么做。这些内容最初需要用Xaml定义。我真的不认为这是你需要担心的事情。事实上,如果是这样的话,你最好考虑密集使用数据模板。@AnthonyWJones:不确定密集的需求。我将在页面上添加10到100个项目,包括形状、路径、网格、文本框、自定义字体、图像等。每一个项目都可能进行多种操作,例如,不仅是一个矩形,还有一个带有着色器的矩形、imagebrush填充、imagebrush上的变换等。。我只是想知道什么可能是最快的。如果你是说基于我刚才描述的,XAML和代码隐藏是相同/相似的,那很酷,我只是不了解我自己。御宅族:最好先了解一下什么是真正有效的,然后让软件真正编写出来。然后考虑它是否足够快。说真的,你要花多少时间来研究如何对抗这个系统,因为你没有在诺米纳做任何事情
l way将比发现您需要将一些Xaml重新编写为代码更痛苦,这是一个简单的操作转换练习。即使您预先知道它需要的是代码而不是Xaml,我仍然会先在Xaml中执行,让它工作,然后将Xaml位移植到代码中。顺便说一句,10-100项是微不足道的。@AnthonyWJones:洞察力很好,尤其是10-100项不重要。我有自动写入XAML的功能,它似乎运行得很好,我只是不确定我是否错过了一个大好机会。XAML是非常舒适的,它的代码隐藏在我的手中感觉不稳定,但我现在正在探索各种选择,因为在某些情况下,例如,我仍然需要一些代码隐藏。哇,这非常有趣!你如何测试速度上的差异?@Otaku:除了我在简单测试中所做的方式,我真的不能再多说如何测试速度上的差异了。在我的测试中,我有一个循环,我计算开始和结束之间的延迟时间。在大多数情况下,速度上的唯一差异发生在实例化时。与对象的交互不受影响。如果你有很多对象和动态动画/效果正在生成,那么在代码中实例化它们可能是有效的。我不同意。只要看看使用代码来完成通常在Xaml中实现的事情,如果已经确定使用Xaml速度太慢,您可以为如此密集地创建组件的原因辩护,并且您使用DataTemplate来实例化一组对象比代码慢。@AnthonyWJones:我想我说的没有什么不同。我说使用代码是有效的,我没有说使用代码永远是正确的。在这些情况下考虑使用代码是有效的,如果我不知道真正的要求和考虑了什么样的解决方案,我就不能通过裁决。也许这个词应该用“考虑”这个词来代替。哇,太有趣了!你如何测试速度上的差异?@Otaku:除了我在简单测试中所做的方式,我真的不能再多说如何测试速度上的差异了。在我的测试中,我有一个循环,我计算开始和结束之间的延迟时间。在大多数情况下,速度上的唯一差异发生在实例化时。与对象的交互不受影响。如果你有很多对象和动态动画/效果正在生成,那么在代码中实例化它们可能是有效的。我不同意。只要看看使用代码来完成通常在Xaml中实现的事情,如果已经确定使用Xaml速度太慢,您可以为如此密集地创建组件的原因辩护,并且您使用DataTemplate来实例化一组对象比代码慢。@AnthonyWJones:我想我说的没有什么不同。我说使用代码是有效的,我没有说使用代码永远是正确的。在这些情况下考虑使用代码是有效的,如果我不知道真正的要求和考虑了什么样的解决方案,我就不能通过裁决。也许这个词应该用“考虑”这个词来代替。除非你对我的问题有一个答案,否则这不是一个答案,它更适合作为对你不同意的答案的评论。除非你对我的问题有一个答案,否则这不是一个答案,它更适合作为你不同意的答案的评论。
#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")));
        }
    }
}