简体   繁体   English

XAML与在运行时创建的控件

[英]XAML vs. controls created at runtime

In Windows Phone, what is better from performance point of view, to define controls in XAML or creating them dynamically at runtime? 在Windows Phone中,从性能的角度来看,在XAML中定义控件还是在运行时动态创建控件更好? What are cons/pros for XAML and runtime controls? 什么是XAML和运行时控件的缺点/优点?

I remember reading that Xaml loading is more efficient than the equivalent creation in code, however: 我记得读过Xaml加载比代码中的等效创建更有效,但是:

Factors that effect the speed included the number of named elements (as they get connected to code-behind members by lookups in the designer file) and the number of similar/total elements. 影响速度的因素包括命名元素的数量(因为它们通过设计器文件中的查找连接到代码隐藏成员)以及相似/总元素的数量。

From a business perspective keeping it in Xaml is a far easier way to maintain a project. 从业务角度来看,将其保留在Xaml中是一种更容易维护项目的方法。 The whole purpose of Xaml was to allow designers to work on the look independent of the coders. Xaml的全部目的是让设计师能够独立于编码器工作。

I think i'm right in saying that perf wise it does not matter. 我认为我是正确的说这是完全无关紧要的。

From a Dev perspective, having the controls defined in the XAML allows a clear separation, and if you are using MVVM, you get all the benefits that pattern brings (unit testing, clear separation, better manageability etc) 从Dev的角度来看,拥有XAML中定义的控件可以实现清晰的分离,如果使用MVVM,您将获得模式带来的所有好处(单元测试,清晰分离,更好的可管理性等)

also, if you have your controls defined in XAML, your app will lend itself better to Blend development and allow you to control the look and feel of your app visually. 此外,如果您在XAML中定义了控件,您的应用程序将更适合Blend开发,并允许您直观地控制应用程序的外观。

I prefer to keep all UI controls in XAML, and do the nuts and bolts in code..but the framework is designed to suit all dev tastes 我更喜欢在XAML中保留所有UI控件,并在代码中执行细节。但框架的设计适合所有开发人员的口味

I think if you try to define animations/VisualStates using pure code, you'll realize how great Blend can be. 我想如果你尝试使用纯代码定义动画/ VisualStates,你会发现Blend有多棒。

The Binding syntax also, doesn't feel very readable in code, but then, that's maybe a matter of taste. Binding语法也在代码中感觉不太可读,但是,这可能是一个品味问题。

The performance difference is negligible and shouldn't even be a factor because the maintenance and code quality difference is so large. 性能差异可以忽略不计,甚至不应成为一个因素,因为维护和代码质量差异如此之大。 Code-generated controls are a nightmare to maintain, are difficult to design and style, and make it harder for developers to follow the application flow. 代码生成的控件是维护的噩梦,难以设计和样式化,并且使开发人员更难以遵循应用程序流程。 You basically are asking about the difference between using XAML which can provide design-time views, separate presentation logic from business logic, and allows tools like Blend to take over, versus a programmatic approach that negates all of the above. 您基本上都在询问使用XAML(可以提供设计时视图),单独的表示逻辑与业务逻辑之间的区别,以及允许Blend等工具接管,而不是使用上述所有方法的编程方法。 What if you decided down the road to port your application to Windows 8 and use the WinRT engine? 如果您决定将应用程序移植到Windows 8并使用WinRT引擎,该怎么办? With the XAML approach and a pattern like MVVM you could likely reuse the bulk of your code and simply update some of the views, whereas with a programmatic approach all of that code would have to be trashed in favor of the new runtime. 使用XAML方法和类似MVVM的模式,您可以重用大部分代码并简单地更新一些视图,而使用编程方法,所有代码都必须被抛弃以支持新的运行时。 So even if there was a slight performance difference between one over the other, I'd suggest it doesn't matter because the other factors are far more important to consider. 因此,即使两者之间存在轻微的性能差异,我也认为这并不重要,因为其他因素要考虑得更为重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM