0

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:

namespace RealityIncognita
{
    partial class MainWindow
    {

Can I do the same with xaml files?

That's the header that main (and only) window has:

<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:

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:

windowRoom.Children.Remove(areaRoom);
windowMain.Children.Add(areaRoom);
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

EvgenieT
  • 209
  • 1
  • 4
  • 16
  • Does http://blog.spinthemoose.com/2013/03/24/disable-the-xaml-designer-in-visual-studio/ help for 3)? – mjwills Feb 26 '18 at 09:01
  • Thank you, will try it from home. EDIT: As I understand that will allow me to use xaml "blindly". That's what I am doing anyway. I need to have visual access. – EvgenieT Feb 26 '18 at 09:03
  • Sounds like it would be better to move game room related xaml and logic to separate user control. That's what you usually do in C# too - you don't put the same class in a bunch of partial files, but instead you separate your logic among multiple different classes and compose them. – Evk Feb 26 '18 at 09:05
  • Are you saying your entire application is all defined within a single XAML file? Why? Break it out as separate components or controls. Add them as resources to your app. – Jeff Mercado Feb 26 '18 at 09:05
  • When I started, I barely new anything of C#. Now I have a hard time turning the existing code to something better organized. And that's exactly what I am trying to do, and asking for details here. – EvgenieT Feb 26 '18 at 09:29
  • Basically, you shouldn't have much more in a single file, than what is displayed at the same time. However, you didn't disclose much of your project structure, so I have no idea what would be a good starting point in refactoring your case. – grek40 Feb 26 '18 at 09:38
  • There is a main window. Inside it is Viewbox. Inside Viewbox is the largest grid that includes everything else. The deeper level is a number of smaller grids, where each grid is a game room. Each such grid \ room has a number of Images, Buttons and MediaElements. – EvgenieT Feb 26 '18 at 09:42
  • What happened to answer that was posted some time ago? – EvgenieT Feb 26 '18 at 10:23
  • Can this help me: https://stackoverflow.com/questions/4977872/split-one-big-xaml-in-number-of-sub-xaml-files ? – EvgenieT Feb 26 '18 at 11:46
  • @EvgenieT yes it can help you. – grek40 Feb 26 '18 at 12:05
  • What about event handlers from UserControls? Will they be accessible from main window? – EvgenieT Feb 26 '18 at 12:08

0 Answers0