Ejemplo n.º 1
0
        private void Serialize_Click(object sender, RoutedEventArgs e)
        {
            AllModels allModels = new AllModels(appViewModel.ModulesTab.Modules, appViewModel.VehiclesTab.Vehicles);

            string fileName      = "";
            string fileExtension = "";//StringBuilder
            string filter        = string.Join("|", Serializers.Select(x => x.Format));

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = filter;
            if (saveFileDialog.ShowDialog() == true)
            {
                fileName = saveFileDialog.FileName;
            }
            //fileName = System.IO.Path.GetFileName(fileName);
            fileExtension = System.IO.Path.GetExtension(fileName);


            //CivilianVehicle testVehicle = new CivilianVehicle()
            //{
            //    Engine = new Engine()
            //    {
            //        Name = "Engine1",
            //        Power = 100,
            //        Vulnerability = "Good"
            //    },
            //    Name = "Civilian",
            //    Price = 200,
            //    SeatsCount = 3,
            //    Weight = "Big",
            //    Tracks = new Tracks()
            //    {
            //        Name = "Tracks1",
            //        Vulnerability = "Bad",
            //        WeightCapacity = 300
            //    }
            //};



            foreach (ISerializers serializer in Serializers)
            {
                string serializerExtension = serializer.Format.Substring(serializer.Format.LastIndexOf('.'), serializer.Format.Length - serializer.Format.LastIndexOf('.'));

                if (serializerExtension == fileExtension)
                {
                    SelectedSerializer = serializer;
                    SelectedSerializer.SerializeObject(allModels, fileName);
                    break;
                }
            }

            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);


                foreach (var plugin in Plugins)
                {
                    if (comboBoxPlugins.SelectedItem == plugin.Key)
                    {
                        plugin.Value.Archive(file);
                        file.Close();
                        File.Delete(fileName);
                    }
                }
            }
            catch
            { }
        }