WPF性能:使用XML进行静态控制和以编程方式进行控制之间的区别? [英] WPF Performance : difference between using XML for static controls and making controls programmatically ?

查看:85
本文介绍了WPF性能:使用XML进行静态控制和以编程方式进行控制之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用WPF,并开始使用一些游戏和视觉应用程序。



在我的第一个应用程序中,我开始了一个空白项目,然后创建了一个继承自窗口,我添加了一个画布并从动态删除图像,如下所示:



  class  MainWindow:Window 
{
public Canvas canvas = new 画布();

public MainWindow()
{
this .addChild(帆布);
}

// 每隔一段时间添加一张图片并移除它
}





这对于静态XML是不可能的,但有人告诉我做控件是个坏主意动态,这是真的吗?这是性能损失吗?是否有一种简单而有效的方法来绘制,比方说,30 fps的100张图像没有滞后?

解决方案

没有一个控件真的是静态的。没有任何运行时差异。



最有可能的是,使用XML意味着XAML。 XAML仅用作数据源,用于实际生成C#代码(或VB.NET或其他.NET语言中的代码)进行编译,并且与以编程方式完全相同的方式使用。



您可以在工作中使用这一事实。如果您知道如何使用XAML执行某些操作但不知道如何在C#代码中执行类似操作,请执行以下操作:开发基于XAML的项目并构建它。然后在项目文件所在的目录下搜索* .cs文件。你会发现一些* .cs文件,这些文件不在你的源代码中。这些文件是使用XAML自动生成的。看他们。你将能够了解它的工作原理。



祝你好运,

-SA

I have started using WPF and started with some games and visual apps.

In my first App I started a blank project then made a class that inherited from a window which had a canvas that I added and removed images from Dynamically, like follows:

class MainWindow : Window
{
    public Canvas canvas=new Canvas();

    public MainWindow()
    {
    this.addChild(canvas);
    }

    //add an image every second move it and remove it
}



This would be impossible with static XML but someone told me it is a bad idea to do controls dynamically, is it true? Is it a performance loss? And is there a simple and efficient method to draw, lets say, 100 images at a 30 fps without lag?

解决方案

None of the controls are really "static". There is no any runtime difference.

Most likely, by "using XML" you meant XAML. XAML merely serves as a data source use to actually generate C# code (or the code in VB.NET or other .NET language) to compile is and to use exactly in the same was as you would do it "programmatically".

You can use this fact in your work. If you know how to do certain things with XAML but don't know how to do similar thing in C# code, do the following: develop XAML-based project and build it. Then perform the search for *.cs files under the directory where your project file is located. You will find some *.cs files which was not in your source code. Those files were auto-generated with the use of XAML. Look at them. You will be able to learn how it works behind the hood.

Good luck,
—SA


这篇关于WPF性能:使用XML进行静态控制和以编程方式进行控制之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆