I am designing a minesweeper kind of layout for a game in silverlight. Currently, I have used a square
user control on the Canvas
control. There are properties assigned to it.
I also want to add a functionality at a later stage. It is about curving using the bezier curves and plotting those squares on the curve with the x,y co-ordinates instead of looping through. Then I want to send the square's x,y position in an XML file.
My question is that what way should I use that will be the best optimized combination in terms of least memory consumption, fast and efficient performance as well as easy implementation. If you experts have any other ideas, please let me know. Thanks in advance.!
If you can draw everything as shapes and images (i.e. vector graphics) and you'll be able to take advantage of the hardware acceleration for the graphical elements. Also, you'll get better performance if you don't define your squares as UserControls, you should be creating them dynamically in code as shapes containing other shapes and then have an object model that is linked to the shapes based on their position (e.g. Dictionary squares; Dictionary squareElements).
In terms of memory consumption and file access you should keep the square's x,y position in memory as a Point strucutre and serialize to file (XML is fine) only when you need to (i.e. when the player leaves the game).
Well, over the days, I sorted out the answer myself. I continued through my Square
user control and used it in the layout.
About plotting of the x/y positions, I used this:
Point point = myElement.TransformToVisual(App.Current.RootVisual as FrameworkElement)).Transform(new Point(0, 0));
There was a glitch in saving XML file because silverlight 4 does not give elevated privileges for in-browser application. But then I used this on my save button click event:
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.DefaultExt = "xml";
dlgSave.Filter = "XML Files (XML)|*.xml;";
dlgSave.FilterIndex = 1;
strXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + myXML.ToString();//myXML is the XDocument I created globally and saved data in it
try
{
bool check = (bool)dlgSave.ShowDialog();
if (check)
{
using (Stream stream = dlgSave.OpenFile())
{
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
sw.Write(strXML);
sw.Close();
stream.Close();
}
MessageBox.Show("XML Saved successfully");
}
catch (SecurityException)
{
MessageBox.Show("There seems to be an issue with saving XML file on the disk, please try again...", "Something's not right", MessageBoxButton.OK);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Saving here requires authorised permissions", "Access Denied", MessageBoxButton.OK);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With