Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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?
Posted

1 solution

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
 
Share this answer
 
v2
Comments
d3thknight 26-Jun-13 16:43pm    
OK, i get it and i have used the WPF project template to see how it works. But a question about performance, will i be able to realistically display 200 image controls and update them at about 30-50 fps?
Sergey Alexandrovich Kryukov 26-Jun-13 16:56pm    
As there is not a difference in the technique, from the runtime perspective, there is no difference between performance. Everything else depends on the quality of your code. In principle, you can beat the Microsoft code generator, especially in special cases. However, generation of the UI should not be a bottleneck, if your bigger architecture if good enough. That said, you don't really need top performance, you only need sufficient performance, then you won't see the difference. Are you getting the idea?

OK, are you accepting the answer formally (green button)?

—SA
d3thknight 26-Jun-13 19:40pm    
OK, thanx for the info, marked your answer as the solution.
Sergey Alexandrovich Kryukov 26-Jun-13 19:44pm    
You are very welcome.
Good luck, call again.
—SA
Uday P.Singh 27-Jun-13 9:02am    
agree 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900