Exemple #1
0
 public static string XMLSerialize(DishList list)
 {
     try
     {
         XmlSerializer serializer = new XmlSerializer(typeof(DishList), new XmlRootAttribute("dish_list"));
         using (FileStream fs = new FileStream(XMLPath, FileMode.Create))
         {
             serializer.Serialize(fs, list);
         }
         return(XMLPath);
     }
     catch
     {
         return(null);
     }
 }
Exemple #2
0
 public static DishList BinaryDeserialize()
 {
     try
     {
         BinaryFormatter formatter = new BinaryFormatter();
         var             list      = new DishList();
         using (FileStream fs = new FileStream(BinPath, FileMode.Open))
         {
             list = (DishList)formatter.Deserialize(fs);
         }
         return(list);
     }
     catch
     {
         return(null);
     }
 }
Exemple #3
0
 public static string BinarySerialize(DishList list)
 {
     try
     {
         BinaryFormatter formatter = new BinaryFormatter();
         using (FileStream fs = new FileStream(BinPath, FileMode.Create))
         {
             formatter.Serialize(fs, list);
             fs.Flush();
         }
         return(BinPath);
     }
     catch
     {
         return(null);
     }
 }
Exemple #4
0
 public static DishList XMLDeserialize()
 {
     try
     {
         var           list       = new DishList();
         XmlSerializer serializer = new XmlSerializer(typeof(DishList), new XmlRootAttribute("dish_list"));
         using (FileStream fs = new FileStream(XMLPath, FileMode.Open))
         {
             list = (DishList)serializer.Deserialize(fs);
         }
         return(list);
     }
     catch
     {
         return(null);
     }
 }
        public MainWindow()
        {
            InitializeComponent();
            Menu = new DishList();
            Menu.FillList();
            dishGrid.DataContext       = Menu;
            ingredientGrid.DataContext = Ingredients;

            var ingredients = new ObservableCollection <Ingredient>()
            {
                new DairyProduct("Cow milk", 0.6f, DairyProduct.Group.Milk, 250),
                new DairyProduct("Vanilla ice cream", 2.01f, DairyProduct.Group.IceCream, 100),
                new Fruit("Banana", 0.89f, "Yellow", 0.12f, 150),
                new Vegetable("Tomato", 0.3f, Vegetable.PungencyDegree.None, "Red", 150),
                new Meat("Chiken", 1f, Meat.MeatType.Chicken, 100),
                new Meat("Beef", 1f, Meat.MeatType.Beef, 100)
            };

            FullList = new IngredientList(ingredients);
            pluginCheckBox.DataContext = this;
            fullListGrid.ItemsSource   = FullList.Ingredients;
            FillPluginList();
        }
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDlg = new OpenFileDialog();

            fileDlg.Filter = "Archive files|*.zip;*.gz";
            fileDlg.ShowDialog();
            string path = fileDlg.FileName;

            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            if (PluginList.Count == 0)
            {
                var result = MessageBox.Show("Plugins cannot be found. Please choose plugin directory.", "Error", MessageBoxButton.YesNo);
                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
                FillPluginList();
            }
            else
            {
                string msg = string.Empty;
                foreach (IPlugin plugin in PluginList)
                {
                    if (plugin.ArchiveType == Path.GetExtension(path))
                    {
                        try
                        {
                            string resPath = plugin.Decompress(path);
                            path = Path.GetFullPath(resPath);
                            var      ext  = Path.GetExtension(path);
                            DishList list = null;
                            switch (ext)
                            {
                            case ".dat":
                                list = Serializator.BinaryDeserialize();
                                break;

                            case ".xml":
                                list = Serializator.XMLDeserialize();
                                break;
                            }

                            if (list != null)
                            {
                                Menu = list;
                                RefreshMenuGrid(dishGrid, Menu);
                                msg = "Successfully decompressed.";
                            }
                            else
                            {
                                msg = "Deserialization error";
                            }
                        }
                        catch (Exception ex)
                        {
                            msg = "File cannot be decompressed: " + ex.Message;
                        }
                    }
                }
                MessageBox.Show(msg, "Decompression");
            }
        }
 public void RefreshMenuGrid(DataGrid grid, DishList list)
 {
     grid.DataContext = typeof(DishList);
     grid.DataContext = list;
 }