Exemplo n.º 1
0
        /// <summary>
        ///     Applies values from passed entity. Used for loading data from file.
        /// </summary>
        /// <param name="entity">Object holding values to apply.</param>
        /// <exception cref="CorruptedFileException">Thrown when passed object is incomplete or null.</exception>
        public void Deserialize(SequencerEntity entity)
        {
            if (entity?.Sequences == null)
            {
                throw new CorruptedFileException();
            }

            while (Sequences.Count > 0)
            {
                RemoveSequence(Sequences[0]);
            }

            var win = (MainWindow)Window.GetWindow(this);

            if (win != null)
            {
                win.BpmBox.Text = entity.Bpm.ToString();
            }

            ClockSequence.Deserialize(entity.Clock);

            foreach (var sequenceEntity in entity.Sequences)
            {
                (typeof(Sequencer).GetMethod("AddSequence")
                 ?.MakeGenericMethod(Type.GetType(sequenceEntity.Type))
                 .Invoke(this, null) as Sequence)?.Deserialize(sequenceEntity);
            }

            (ClockSequence as ClockSequence)?.ReloadTimer();
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Serializes and saves project to XML file.
        /// </summary>
        /// <param name="fileName">Name of file to create/overwrite.</param>
        public void Save(string fileName)
        {
            var entity = new SequencerEntity
            {
                Bpm       = Bpm,
                Clock     = ClockSequence.Serialize() as ClockSequenceEntity,
                Sequences = Sequences.Select(seq => seq.Serialize()).ToArray()
            };

            using (var stream = File.Create(fileName))
            {
                var serializer = new XmlSerializer(typeof(SequencerEntity));
                serializer.Serialize(stream, entity);
            }
        }