c#


Loading a grid in xaml code from a file, then making it active in main code


I am making a video game, where each grid serves as game room. Each grid\room has a large number of objects like things that can be used, images and sound files. Until now, they all were stored in one large file. But now, I was told that this approach wastes a lot of resources.
My plan is, to store xaml code of every such grid as a separate file, then load relevant file at run time with usual code.
For now xaml looks about like this:
<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">
<Viewbox Stretch="Fill">
<Grid x:Name="areaContainer" HorizontalAlignment="Left" Height="900"
VerticalAlignment="Top" Width="1600">
<Grid x:Name="areaMain">
<Grid.Background>
<ImageBrush
ImageSource="Resources/Images/Interface/main_interface.jpg"/>
</Grid.Background>
<Grid x:Name="areaShowers" HorizontalAlignment="Left" Height="700"
Margin="1653,790,-1561,-590" VerticalAlignment="Top" Width="1508"
IsVisibleChanged="areaShowers_IsVisibleChanged">
Here's example - areaShowers is a grid for relevant room. Until now, it was stored in the main file, like all other grids, and when I needed, I just altered its Margin to put it upon "areaMain" - also a grid.
Now though, I want to put each room into a file, then load it when I need it, and remove it from memory when I don't.
For example I'll create an empty grid "areaGeneric" and add it and it alone to original xaml.
So, I want something like this. Can't provide any earlier attempt, because I don't really know how it can be done.
Grid new_grid = new Grid;
new_grid = load from file (areaRoom.xaml); (file is among the project's
resources)
areaGeneric = new_grid;
Can I load a grid xaml at run-time, then switch grids in main code?
Thank you,
Evgenie
A really simple solution would just be to house each of your rooms inside a user control and then place the control into your container grid when you need to change rooms. Here's the rough idea:
public partial class MainWindow : Window
{
UserControl _currentRoom;
public MainWindow()
{
InitializeComponent();
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
this.areaContainer.Children.Clear();
_currentRoom = null;
if (e.LeftButton == MouseButtonState.Pressed)
_currentRoom = new Room1();
if(e.RightButton == MouseButtonState.Pressed)
_currentRoom = new Room2();
this.areaContainer.Children.Add(_currentRoom);
base.OnPreviewMouseDown(e);
}
}
A "Room":
<UserControl x:Class="Test.Room1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Button Height="100" Width="100">
Hello world!
</Button>
</UserControl>
As for cleaning up your resources - once the user control is removed from the visual tree (assuming you aren't holding a reference) the GC should dispose of the user control. If you wanted to clear your resources more quickly you could implement a dispose method on your rooms and then call that before you change areas.

Related Links

What is the use of a public constructor in a private class
How to modify angle of Unity's transform.forward
Obtain Request Token From REST API using Oauth1 in C#
C# Console App Executed by SQL Agent
How to group LINQ to Entities results by enum and date?
StructureMap registering generic types for using them in injected array collection
How to open or pick multiple images in windows store with PickMultipleFilesAsync() using c#?
ASP.NET Core Web API Throws HTTP 500
How do I change a cell font in google sheets api v4 in C#?
Gridview not sorting and paging for excel sheet in asp
ContentPicker Return null
Could not load file or assembly Dev Express
C# MySQL table list
Converting a DateTime? to DateTime in a setter function in C#
C# yammer api export csv file
What is wrong with my SQLiteParameters?

Categories

HOME
oauth-2.0
qlikview
grizzly
azure-documentdb
domain-driven-design
ldap
dronekit
popover
skypedeveloper
squeak
mailchimp
snmp
glympse
jshell
liquidsoap
pip
wavefront
ejb-3.0
tracking
tmux
javascript-debugger
react-select
responsive
versioning
samba
openam
airbnb
gpo
spring-webflow
summernote
git-branch
symlink
gulp-jshint
dynamic-memory-allocation
bytebuffer
azure-table-storage
jms-topic
multilanguage
akamai
jcr
epub
android-geofence
flex3
azure-storage-files
tapply
redbean
line-endings
cloudconvert
senti-wordnet
overhead
vertex-shader
django-taggit
jmap
nugetgallery
calc
abstract
nslocalizedstring
jquery-draggable
dynamics-nav-2013
javapackager
play-json
om
photogrammetry
data-representation
sicstus-prolog
applinks
grunt-contrib-connect
zoneminder
vundle
isl
twitter-finagle
django-tests
nodeunit
acrofields
mysql-error-1146
dylan
tlbimp
ia-32
friendly-url
cron4j
yii-components
motordriver
reflexil
copyfile
clgeocoder
gmagick
quazip
netbeans7.0
subdirectories
versions
mdd
pixel-bender
opengl-to-opengles

Resources

Encrypt Message