Exemplo n.º 1
0
 private void Serialize(ExchangeHistory exchangeHistory)
 {
     using (Model model = new Model())
     {
         model.ExchangeHistory.Add(exchangeHistory);
         model.SaveChanges();
     }
 }
Exemplo n.º 2
0
        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!";
                }
            }
        }