Evgenie Tivaniuk

Evgenie Tivaniuk

  • NA
  • 82
  • 11.2k

Dividing one large xaml to many smaller ones

Feb 26 2018 4:46 AM
In my project I, until now, kept all the elements in one huge xaml file. Now it became a problem, because too much memory is consumed, and xaml window doesn't even load.
 
All this is during programming itself, there is no problem when running the game. But working with xaml manually is hard.
 
I considered dividing xaml as a possible solution, but I am not sure how to do it.
 
I see two options:
 
1) Do something similar to what I did with CS files: I divided the code to smaller files, but made them all of same class. That's what I put in each file:
  1. namespace RealityIncognita { partial class MainWindow {  
Can I do the same with xaml files?
 
That's the header that main (and only) window has:
  1. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Name="wdwMain" x:Class="RealityIncognita.MainWindow" Height="900" Width="1600" ResizeMode="NoResize" WindowState="Maximized" Cursor="Cross" WindowStyle="None" Loaded="wdwMain_Loaded" HorizontalAlignment="Center" VerticalAlignment="Center" WindowStartupLocation="CenterScreen" > <Viewbox x:Name="viewMain" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5"> <Grid x:Name="areaContainer" HorizontalAlignment="Left" Height="900" VerticalAlignment="Top" Width="1600">  
I want to put each game room (grid) as different xaml file, I want them to appear in different windows, but still belong to same class. Will this reduce the memory usage in this case?
 
2) If there is no way to do previous option, I consider another. Again, I want to divide xaml to smaller files, and then, dynamically move relevant grids (with all elements and events) to main window.
 
For example, I want to move relevant grid to physical screen:
  1. Room.Margin = new Thickness(55, 31, 0, 0);  
I move the grid to correct place. Inside the grid, there are more elements, including buttons that have "MouseEnter" and "MouseDown".
 
Now, if I move the whole grid, with all elements during programming, can I later move it dynamically to main window?
 
Will something like that work:
  1. windowRoom.Children.Remove(areaRoom);  
  2. windowMain.Children.Add(areaRoom);  
  3. Room.Margin = new Thickness(55, 31, 0, 0);  
But I am afraid, that this can show errors during compilation, one of windows will have events not connected to cs voids in main code.
 
In one of previous posts, on similar issue, I've been suggested to use MVVM, but I think there are easier ways to achieve what I need, without reprogramming everything. I am very deep in this project, and want to avoid changing too much, if I can help it.
 
And maybe there is a third option:
 
3) I saw that there are many external plugins for Visual Studio. Maybe there is something that will allow VS to use ALL memory available? For now, I get "out of memory exception", while only a part of RAM is consumed. Can I force VS to use more of it?
 
Again: all those problems are during designing xaml, there is no memory leaks during running the game.
 
Thank you, Evgenie

Answers (3)