Page 56 - MSDN Magazine, February 2018
P. 56

Figure 11 C# Implementation of the WebRequestFunction Evaluate Method
Once I extract the rate, date and time, I set them to the corre- sponding labels using the SetText(widgetName, text) function.
In the init function I initialize the data, and I can add additional currencies for the conversion.
It’s easy to have different layouts for different orientations: register orientation change callbacks with the RegisterOrientation- Change function. The on_portrait and on_landscape functions are called each time the device orientation changes. As you can see at the bottom of Figure 10, it’s set up by invoking:
RegisterOrientationChange("on_portrait", "on_landscape");
In general you add widgets to the screen at particular locations, using the logic explained in the “Hello, World!” example in the first section. You probably noticed the different phone back- grounds for landscape and portrait mode. This is done using the SetBackground(imageName) CSCS function.
AddOrSelectTab function has the following signature:
AddOrSelectTab(Tab_Name, Picture_when_active, Picture_when_inactive);
If the the tab doesn’t exist yet, it will be added, otherwise it will be selected and all consecutive widgets will be added to this tab. Figure 12 shows how the tabs look in both active and inactive modes.
Wrapping Up
In this article you saw that with CSCS you can program mobile apps using a scripting language. The script is converted to native code using the C# interpreter and the Xamarin Framework. The CSCS scripts can do anything that can be done in C# (and in Xamarin C# you can do anything that can be done in native app development).
I’ve already published an app written entirely in CSCS. Check out the iOS version at apple.co/2yixGxZ and the Android version at goo.gl/zADtNb.
CSCS scripting for mobile apps is far from complete. To add new functionality to CSCS, you create a new class that derives from the ParserFunction class and override its Evaluate method. Then you register that class with the parser, supplying its CSCS name:
ParserFunction.RegisterFunction("CSCS_Name", new MyNewCustomFunction())
Using CSCS you can place all the widgets programmatically, and the same code will be used for both Android and iOS. And you don’t need to use any XAML for that, as you would with Xamarin.Forms.
You can also combine CSCS with the existing C# code—it’s easy to call C# code from CSCS, as I explained at codemag.com/article/1711081. In that article you can also check the list of functions implemented in CSCS. But for the latest, most up-to-date CSCS functions and features, visit github.com/vassilych/mobile.
Unfortunately, there’s no room to discuss some other cool things you can do in CSCS, such as in-app purchasing and billing, in-app advertisements, scheduling one-shot and repetitive events, and more, but you can check them out in the accompanying source code download. n
Vassili Kaplan is a former Microsoft Lync developer. He is passionate about programming in C#, C++, Python and now in CSCS. He currently lives in Zurich, Switzerland, and works as a freelancer for various banks. You can reach him at iLanguage.ch
ThanKs to the following Microsoft technical expert for reviewing this article: James McCaffrey
C#
public class WebRequestFunction : ParserFunction {
protected override Variable Evaluate(ParsingScript script) {
bool isList = false;
List<Variable> args = Utils.GetArgs(script,
Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name);
string uri = args[0].AsString();
string responseFromServer = "";
WebRequest request = WebRequest.Create(uri);
using (WebResponse response = request.GetResponse()) { Console.WriteLine("{0} status: {1}", uri,
((HttpWebResponse)response).StatusDescription); using (StreamReader sr = new StreamReader(response.GetResponseStream())) {
responseFromServer = sr.ReadToEnd(); }
}
return new Variable(responseFromServer); }
}
also that the WebRequest function is implemented synchronously. You can also make it asynchronous by supplying the callback function to be called when the request is done (analogous to the voice recognition functionality I showed earlier).
Let’s continue analyzing the CSCS code in Figure 10. I was describing what happens in the currency_request function. The JSON response I get from exchangerate-api.com looks like the following:
{"result":"success","timestamp":1511464063,"from":"USD","to":"CHF","rate":0.99045395}
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.
The timestamp is the number of seconds passed since Jan. 1, 1970. The CSCS function Timestamp(format) converts this number of seconds to a specified date or time format.
StrBetween(data, strStart, strEnd) is a convenience function for extracting a substring from the data string between strStart1 and strStart2 strings.
Figure 12 Active and Inactive Tabs on iOS 52 msdn magazine


































































































   54   55   56   57   58