Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I would go about this is to define a class that stores all the information that makes up a slide (and as such, can represent the slide in your code), and then serialize a list of slides, called a Presentation, as an XML file.</p> <p>Each slide is going to have a variable number of controls, which can be images, textboxes, ect, so lets define a control first:</p> <pre><code>public class MySlideClass { public class Control { public Point Position { get; set; } public Size Size { get; set; } public Color Background { get; set; } public Color Forground { get; set; } public Image Picture { get; set; } public string Text { get; set; } public float TextSize { get; set; } public Point TextPosition { get; set; } // ... public float Transparency { get; set; } public bool Visible { get; set; } public Control() { } } public class Slide { [XmlAttribute] public string Name { get; set; } // public string Title { get; set; } public Size Size { get; set; } public Color Background { get; set; } public Color Forground { get; set; } public float Transparency { get; set; } public bool Visible { get; set; } public List&lt;Control&gt; Children { get; set; } public Slide() { } } public MySlideClass() { } } </code></pre> <p>Obviously, you can add/remove properties as your see fit.</p> <p>Note the <code>{ get; set; }</code> and empty default constructor. Your class must consist of public properties (variables defined with the get/set) and have a empty default constructor for the <code>XMLSerializer</code> to be able to serialize your classes (you can still specify other constructors that arn't empty).</p> <p>You would use the classes like so:</p> <pre><code>public void SaveSlideTest() { // New control MySlideClass.Control newControl = new MySlideClass.Control(); newControl.Position = new Point(20,30); newControl.Size = new Size(75,25); newControl.Text = "Image1"; newControl.TextPosition= new Point(0,25); //ctl.Picture = new Bitmap("image1.bmp"); // Add control to children list List&lt;MySlideClass.Control&gt; childrenControls = new List&lt;MySlideClass.Control&gt;(); childrenControls.Add(newControl); // New slide MySlideClass.Slide newSlide = new MySlideClass.Slide(); newSlide.Name = "Slide1"; newSlide.Title = "New Slide"; newSlide.Size = new Size(200,100); // Add child controls to slide newSlide.Children = childrenControls; // Add slide to presentation or 'slideshow' List&lt;MySlideClass.Slide&gt; mySlidePresentation = new List&lt;MySlideClass.Slide&gt;(); mySlidePresentation.Add( newSlide ); // Save presentation to XML SerializeObject("SavedSlidePresentation.xml",mySlidePresentation); } </code></pre> <p>Your SavedSlidePresentation.xml looks like:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ArrayOfSlide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Slide Name="Slide1"&gt; &lt;Title&gt;New Slide&lt;/Title&gt; &lt;Size&gt; &lt;Width&gt;200&lt;/Width&gt; &lt;Height&gt;100&lt;/Height&gt; &lt;/Size&gt; &lt;Background /&gt; &lt;Forground /&gt; &lt;Transparency&gt;0&lt;/Transparency&gt; &lt;Visible&gt;false&lt;/Visible&gt; &lt;Children&gt; &lt;Control&gt; &lt;Position&gt; &lt;X&gt;20&lt;/X&gt; &lt;Y&gt;30&lt;/Y&gt; &lt;/Position&gt; &lt;Size&gt; &lt;Width&gt;75&lt;/Width&gt; &lt;Height&gt;25&lt;/Height&gt; &lt;/Size&gt; &lt;Background /&gt; &lt;Forground /&gt; &lt;Text&gt;Image1&lt;/Text&gt; &lt;TextSize&gt;0&lt;/TextSize&gt; &lt;TextPosition&gt; &lt;X&gt;0&lt;/X&gt; &lt;Y&gt;25&lt;/Y&gt; &lt;/TextPosition&gt; &lt;Transparency&gt;0&lt;/Transparency&gt; &lt;Visible&gt;false&lt;/Visible&gt; &lt;/Control&gt; &lt;/Children&gt; &lt;/Slide&gt; &lt;/ArrayOfSlide&gt; </code></pre> <p>Here are the serialize/deserialize functions:</p> <pre><code>public static List&lt;MySlideClass.Slide&gt; DeserializePresentation(string FileName) { List&lt;MySlideClass.Slide&gt; returnList = new List&lt;MySlideClass.Slide&gt;(); using(StreamReader streamReader = new StreamReader(FileName)) { XmlSerializer xmlReader = new XmlSerializer(typeof(List&lt;MySlideClass.Slide&gt;)); returnList = (List&lt;MySlideClass.Slide&gt;) xmlReader.Deserialize(streamReader); } return returnList; } // A generic object serializer public static void SerializeObject(string Filename,object Obj) { using(StreamWriter streamWriter = new StreamWriter(Filename)) { XmlSerializer xmlSerializer = new XmlSerializer(Obj.GetType()); xmlSerializer.Serialize(streamWriter,Obj); } } </code></pre> <p>This should be more robust than a <code>Dictionary&lt;TKey,TValue&gt;</code>.</p> <p>One important aspect to note: If you decide to add a property to your Slide or Control class, the deserializer will not be able to deserialize the XML files with the older version of the data class.</p> <p>In that case you could either 1) convert the older version XML files manually or otherwise, or 2) Implement the <code>ISerializable</code> interface in your class (reccomended).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload