private void Serialize(ExchangeHistory exchangeHistory) { using (Model model = new Model()) { model.ExchangeHistory.Add(exchangeHistory); model.SaveChanges(); } }
internal void ExchangeButton_Click(object sender, RoutedEventArgs e) { string fromCurrency = ((mainWindow.currencyFrom.Items[0] as ListViewItem).Content as Frame).Name; string toCurrency = ((mainWindow.currencyTo.Items[0] as ListViewItem).Content as Frame).Name; if (fromCurrency == toCurrency) { mainWindow.erroreLabel.Content = "The currency you want to exchange is the same as the currency you want to receive."; } else { mainWindow.erroreLabel.Content = ""; try { double value = Convert.ToDouble(mainWindow.amountBox.Text); Task t = Task.Run(new Action(() => { double from = HttpClientHandler.GetExchangeRateAsync(fromCurrency); double to = HttpClientHandler.GetExchangeRateAsync(toCurrency); mainWindow.Dispatcher.Invoke(new Action(() => { double result = GetResult(value, from, to); mainWindow.resaultLabel.Content = result.ToString(); ExchangeHistory e = new ExchangeHistory(); e.Date = DateTime.Now; e.FromCurrency = fromCurrency; e.FromAmount = value; e.ToCurrency = toCurrency; e.ToAmount = result; Serialize(e); })); })); } catch { mainWindow.erroreLabel.Content = "You entered an incorrect value!"; } } }