예제 #1
0
        private async void ConvertCurrency(string amount, string date)
        {
            ComboBoxItem from = CurrenciesFrom.SelectedItem as ComboBoxItem;

            try
            {
                fromCurrency = (from.Content as String).Substring(0, 3);
            }
            catch
            {
                return;
            }

            ComboBoxItem to = CurrenciesTo.SelectedItem as ComboBoxItem;

            toCurrency = (to.Content as String).Substring(0, 3);

            if ((fromCurrency != lastFromCurrency || toCurrency != lastToCurrency) && (fromCurrency != toCurrency) || (date != lastDate))
            {
                lastDate     = date;
                exchangeRate = await erdp.GetExchangeRate(fromCurrency, toCurrency, date);

                lastFromCurrency = fromCurrency;
                lastToCurrency   = toCurrency;
            }

            if (fromCurrency == toCurrency)
            {
                AmountToText = amount;
            }
            // Checks if AmountFrom textbox is empty or not
            else if (double.TryParse(amount, out double result))
            {
                if (result * exchangeRate > 0.01)
                {
                    AmountToText = string.Format("{0:#,###0.00}", result * exchangeRate);
                }
                else
                {
                    decimal resultInDecimal = (decimal)(result * exchangeRate);
                    AmountToText = resultInDecimal.ToString();
                }
            }
            else
            {
                AmountToText = "";
            }
            AmountFrom.Focus(FocusState.Programmatic);
        }
예제 #2
0
        private async void getCurrencies()
        {
            CurrencyDataProvider cdp = new CurrencyDataProvider();
            Dictionary <string, Models.Currency> currencies;

            try
            {
                currencies = await cdp.GetCurrencies();
            }
            catch
            {
                return;
            }
            ComboBoxItem currenciesFrom;
            ComboBoxItem currenciesTo;

            foreach (var cur in currencies.OrderBy(e => e.Value.CurrencyName))
            {
                if (cur.Value.CurrencySymbol == null)
                {
                    currenciesFrom = new ComboBoxItem {
                        Content = cur.Key + "   " + cur.Value.CurrencyName
                    };
                    currenciesTo = new ComboBoxItem {
                        Content = cur.Key + "   " + cur.Value.CurrencyName
                    };
                }
                else
                {
                    currenciesFrom = new ComboBoxItem {
                        Content = cur.Key + "   " + cur.Value.CurrencyName + " (" + cur.Value.CurrencySymbol + ")"
                    };
                    currenciesTo = new ComboBoxItem {
                        Content = cur.Key + "   " + cur.Value.CurrencyName + " (" + cur.Value.CurrencySymbol + ")"
                    };
                }
                CurrenciesFrom.Items.Add(currenciesFrom);
                CurrenciesTo.Items.Add(currenciesTo);
            }
            CurrenciesFrom.SelectedIndex = 156;
            CurrenciesTo.SelectedIndex   = 143;
            AmountFrom.Focus(FocusState.Programmatic);
        }