Page 46 - MSDN Magazine, February 2018
P. 46
C#
Writing Native Mobile Apps Using a Customizable
Scripting Language
Vassili Kaplan
IntheFebruary2016issueofMSDNMagazine,Ishowed how to create a custom scripting language based on the Split-And- Merge algorithm for parsing mathematical expressions in C# (msdn.com/magazine/mt632273). I called my language Customizable Scripting in C#, or CSCS. Recently, I published an E-book that pro- vided more details about creating a custom language (bit.ly/2yijCod). Creating your own scripting language might not initially seem to be particularly useful, even though there are some interesting applications of it (for example, game cheating). I also found some applications in Unity programming.
But then I discovered an even more interesting application for a customizable scripting language—writing cross-platform applications
formobiledevices.Itturnsoutthatit’spossibletouseCSCStowrite apps for Android and iOS (and Windows Phone can be easily added, as well). And the same code can be used for all platforms. I published an introduction on how to do that in the November-December 2017 issue of CODE Magazine (codemag.com/article/1711081).
In this article I’m going to take a deeper dive and show how to use CSCS to program for mobile devices. I‘ll also correct some inaccu- racies in the CODE Magazine article. You’ll see that anything that can be done on the native platform can be done in CSCS. I’m also going to show how you can add missing features to CSCS on the fly.
To run the code shown in this article, you’ll need Visual Studio 2017 with Xamarin installed, either on Windows or on macOS. I person- ally use Visual Studio Community Edition 2017 on my MacBook. Note that a Mac is needed to deploy iOS apps to the Apple App Store.
A “Hello, World!” for Mobile Apps
Take a look at Figure 1, which shows some basic CSCS code for text- to-speech and voice recognition. Let’s examine the code line by line. The AutoScale function lets you automatically adjust widget size based on the actual device screen size. For instance, with Auto- Scale, the width of a widget will be twice as large on a device with a width of 1280 pixels as on one with a width of 640 pixels. The
actual signature of the AutoScale function is:
AutoScale(scale = 1.0);
If you don’t use the default scale = 1.0 parameter, the specified scale parameter will be applied to the difference. For example, if
This article discusses:
• A “Hello, World!” for mobile apps written in CSCS
• The general structure of Android and iOS projects
• Adding text-to-speech and voice recognition to CSCS
• A currency convertor example to highlight more CSCS features for cross-platform app development
Technologies discussed:
C#, Visual Studio 2017, Xamarin, Android, iOS
Code download available at:
github.com/vassilych/mobile
42 msdn magazine