Evgenie Tivaniuk

Evgenie Tivaniuk

  • NA
  • 82
  • 11.8k

Too many elements in XAML cause "Out of memory" exception

Feb 24 2018 5:28 AM

I am using C# WPF to make a video game. My problem, that I have a huge number of objects, like: Grids, Images, Text, Buttons and MediaPlayer elements. And when I am saying huge, I mean hundreds of elements, maybe even above thousand.

I already learned to disable those elements when the game is running, so there are no problems with game itself, it only uses a little of memory and doesn't stuck. The problem is with using Visual Studio itself. Specifically, with working with Xaml.

By now, my computer simply refuses to allow me working with xaml, both text and visually. The xaml reloads but only shows me the main window. When I try to get to the grid I need, by scrolling to it, or using document outline, I get "Out of memory exception". Until now, when there were less elements, I could "bypass" this problem by making some of elements non-visible (eye icon in document outline), or by setting higher priority with task manager. I also can create new game rooms (grids) in another window, then just copy-paste xaml code, it does help, but I want to fix the problem, not find the way around.

I am almost sure, that it happens because I keep all the elements in the same XAML file. However, I don't know how to do alternatives.

Can you please suggest, how to solve this problem?

What is important: I want to stay with more or less same structure of my project. I mean I don't want to reprogram everything already finished, and absolutely don't want to convert to Unity.

If I can make a guess: Is it possible, let all xaml elements stay in one window, but not LOAD them, as long as I set them to "Hidden"?

Also, someone recomended to use MVVM. I saw a few examples, but I still hardly understand what it is, and how to use it specifically in my case. 

If not, I'll be happy to hear any suggestions.

Since the problem isn't with coding, there is no example of code I can provide.

Thank you,

Evgenie

Answers (1)