Page 53 - MSDN Magazine, February 2018
P. 53
Figure 8 Currency Convertor in Portrait Mode on iPhone (Left) and on Android (Right)
Functions on_about and on_refresh are both callbacks that happen when the user clicks on a button.
The on_about method is executed when the user clicks on the “Powered by” button in the Settings tab, which causes the OpenUrl function to open the exchangerate-api.com homepage in the default browser (this tab isn’t shown in Figure 8 and Figure 9). The on_refresh method is executed when the user clicks on the Convert button. You then get the selected currencies and the CSCS currency_request function is invoked, which does the actual rate conversion.
To get up-to-date exchange rates in your app, you have to use an online service. I chose exchangerate-api.com.
I didn’t have a callback function in text-to-speech, but you can add one in a similar way to tell the user when the speech completes (or if there’s an error). The callback to the CSCS code is performed by invoking the UIVariable.GetAction method:
public static Variable GetAction(string funcName, string senderName, string eventArg)
{
if (senderName == "") { senderName = "\"\"";
}
if (eventArg == "") {
eventArg = "\"\""; }
string body = string.Format("{0}({1},{2});", funcName, senderName, eventArg);
ParsingScript tempScript = new ParsingScript(body); Variable result = tempScript.ExecuteTo();
return result; }
You can see how this function is used in Figure 7.
Example: A Currency Convertor
As an example of using different CSCS features for cross-platform app development, let’s create an app from scratch—a currency convertor.
To get up-to-date exchange rates in your app, you have to use an online service. I chose exchangerate-api.com. The site provides an easy-to-use Web service where the first 1,000 requests per month are free, which should be enough to start. After registration you get a unique key you must supply with each request.
My app has different views in portrait and landscape modes. Figure 8 shows portrait mode and Figure 9 shows landscape mode for both iPhone and Android.
Figure 10 contains the entire CSCS implementation of the currency convertor app.
msdnmagazine.com
The currency_request function first checks if both currencies are the same—in this case I already know that the rate is 1 and there’s no need to call a Web service (I want to save my free limited uses
Figure 9 Currency Convertor in Landscape Mode on iPhone (Top) and on Android (Bottom)
February 2018 49