Page 54 - MSDN Magazine, February 2018
P. 54

of this service per month). Otherwise, the WebRequest function is called. This function is common to both iOS and for Android and its implementation is shown in Figure 11. Note that you don’t
Figure 10 CSCS Implementation of the Currency Convertor App
have to do the exception handling in C# code. If an exception is thrown (for example, if the service is unavailable), the exception will be propagated to the CSCS code, where it will be caught. Note
function on_about(sender, arg) { OpenUrl("http://www.exchangerate-api.com");
}
function on_refresh(sender, arg) { currency1 = GetText(cbCurrency1); currency2 = GetText(cbCurrency2); currency_request(currency1, currency2);
}
function currency_request(currency1, currency2) { if (currency1 == currency2) {
time = Now("HH:mm:ss"); date = Now("yyyy/MM/dd"); rate = 1;
} else {
url = apiUrl + currency1 + "/" + currency2; try {
data = WebRequest(url); } catch(exception) {
WriteConsole(exception.Stack); ShowToast("Couldn't get rates. " + exception); SetText(labelRateValue, "Error");
return;
}
try {
timestamp = StrBetween(data, "\"timestamp\":", ","); time = Timestamp(timestamp, "HH:mm:ss");
date = Timestamp(timestamp, "yyyy/MM/dd"); rate = StrBetween(data, "\"rate\":", "}");
} catch(exception) {
ShowToast("Couldn't get rates. " + exception); SetText(labelRateValue, "Error");
return;
} }
SetText(labelRateValue, rate); SetText(labelDateValue, date); SetText(labelTimeValue, time);
}
function init() {
currencies = {"EUR", "USD", "GBP", "CHF", "JPY", "CNY", "MXN", "RUB", "BRL", "SAR"}; flags = {"eu_EU", "en_US", "en_GB", "de_CH", "ja_JP", "zh_CN",
"es_MX", "ru_RU", "pt_BR", "ar_SA"};
AddWidgetData(cbCurrency1, currencies); AddWidgetImages(cbCurrency1, flags); SetSize(cbCurrency1, 80, 40); SetText(cbCurrency1, "USD");
AddWidgetData(cbCurrency2, currencies); AddWidgetImages(cbCurrency2, flags); SetSize(cbCurrency2, 80, 40); SetText(cbCurrency2, "MXN");
}
SetImage(buttonRefresh, AddAction(buttonRefresh, SetFontColor(buttonRefresh, SetFontSize(buttonRefresh,
AddAction(aboutButton,
"coins"); "on_refresh"); "white"); 20);
"on_about");
function on_portrait(sender, arg) {
AddOrSelectTab("Rates", "rates_active.png", "rates_inactive.png"); SetBackground("us_bg.png");
locCurrency1 = GetLocation("ROOT", "LEFT", "ROOT", "TOP", 10, 80); AddCombobox(locCurrency1, "cbCurrency1", "", 280, 100);
locCurrency2 = GetLocation("ROOT", "RIGHT", cbCurrency1, "CENTER", -10); AddCombobox(locCurrency2, "cbCurrency2", "", 280, 100);
locRateLabel = GetLocation("ROOT", "CENTER", cbCurrency2, "BOTTOM", -80, 60); AddLabel(locRateLabel, "labelRate", "Rate:", 200, 80);
locRateValue = GetLocation("ROOT", "CENTER", labelRate, "CENTER", 100); AddLabel(locRateValue, "labelRateValue", "", 240, 80);
locDateLabel = GetLocation("ROOT", "CENTER", labelRate, "BOTTOM", -80); AddLabel(locDateLabel, "labelDate", "Date:", 200, 80);
locDateValue = GetLocation("ROOT", "CENTER", labelDate, "CENTER", 100); AddLabel(locDateValue, "labelDateValue", "", 240, 80);
locTimeLabel = GetLocation("ROOT", "CENTER", labelDate, "BOTTOM", -80); AddLabel(locTimeLabel, "labelTime", "Time:", 200, 80);
locTimeValue = GetLocation("ROOT", "CENTER", labelTime, "CENTER", 100); AddLabel(locTimeValue, "labelTimeValue", "", 240, 80);
locRefresh = GetLocation("ROOT", "CENTER", "ROOT", "BOTTOM", 0, -4); AddButton(locRefresh, "buttonRefresh", "Convert", 200, 100);
AddOrSelectTab("Settings", "settings_active.png", "settings_inactive.png"); locAbout = GetLocation("ROOT", "CENTER", "ROOT", "BOTTOM", -4); AddButton(locAbout, "aboutButton", "Powered by exchangerate-api.com", 360, 100);
}
function on_landscape(sender, arg) {
AddOrSelectTab("Rates", "rates_active.png", "rates_inactive.png"); SetBackground("us_w_bg.png");
locCurrency1 = GetLocation("ROOT", "LEFT", "ROOT", "CENTER", 50); AddCombobox(locCurrency1, "cbCurrency1", "", 200, 120);
locCurrency2 = GetLocation(cbCurrency1, "RIGHT", "ROOT", "CENTER", 40); AddCombobox(locCurrency2, "cbCurrency2", "", 200, 120);
locDateLabel = GetLocation(cbCurrency2, "RIGHT", "ROOT", "CENTER", 60); AddLabel(locDateLabel, "labelDate", "Date:", 180, 80);
locDateValue = GetLocation(labelDate, "RIGHT", labelDate, "CENTER", 10); AddLabel(locDateValue, "labelDateValue", "", 220, 80);
locRateLabel = GetLocation(cbCurrency2, "RIGHT", labelDate, "TOP", 60); AddLabel(locRateLabel, "labelRate", "Rate:", 180, 80);
locRateValue = GetLocation(labelRate, "RIGHT", labelRate, "CENTER", 10); AddLabel(locRateValue, "labelRateValue", "", 220, 80);
locTimeLabel = GetLocation(cbCurrency2, "RIGHT", labelDate, "BOTTOM", 60); AddLabel(locTimeLabel, "labelTime", "Time:", 180, 80);
locTimeValue = GetLocation(labelTime, "RIGHT", labelTime, "CENTER", 10); AddLabel(locTimeValue, "labelTimeValue", "", 220, 80);
locRefresh = GetLocation("ROOT", "CENTER", "ROOT", "BOTTOM", 0, -4); AddButton(locRefresh, "buttonRefresh", "Convert", 180, 90);
AddOrSelectTab("Settings", "settings_active.png", "settings_inactive.png"); locAbout = GetLocation("ROOT", "CENTER", "ROOT", "BOTTOM", -4); AddButton(locAbout, "aboutButton", "Powered by exchangerate-api.com", 360, 100);
}
AutoScale();
apiUrl = "https://v3.exchangerate-api.com/pair/c2cd68c6d7b852231b6d69ee/";
RegisterOrientationChange("on_portrait", "on_landscape"); init();
if (Orientation == "Portrait") { on_portrait("", "");
} else { on_landscape("", "");
} SelectTab(0);
50 msdn magazine
C#


































































































   52   53   54   55   56