Page 48 - MSDN Magazine, April 2018
P. 48

VISUAL STUDIO FOR MAC
Programming for
watchOS with Xamarin
and Visual Studio for Mac
Dawid Borycki
Small wearable devices like personal activity trackers and smart watches (such as Microsoft Band, Android Wear or Apple Watch) are becoming more and more popular. These wearables are equipped with various sensors, which monitor wearer’s health parameters in real time. Many wearables also have communication interfaces, so sensor data can be easily transmitted to custom or dedicated cloud services (such as Microsoft Health) for storage or advanced process- ing. As a result, the wearable can act as an additional endpoint in an Internet of Things (IoT) ecosystem. This, in turn, can help advance personal health care to a new level, where IoT predictive algorithms can inform the user about emerging health issues in advance.
Wearables can also run custom apps. Developers are provided with dedicated SDKs. However, as is the case for many mobile devices, each platform has its own specific API, which can be accessed through platform-specific programming languages and tools. To make things easier, Xamarin provides support for Android Wear
and watchOS within the Xamarin.Android and Xamarin.iOS libraries, respectively. You can develop wearable apps in a similar manner as mobile apps, by utilizing the common .NET code base, which is referenced in the platform-specific projects.
In this article, I’ll show you how to utilize such an approach to build the watchOS app depicted in Figure 1. When you run this app, it begins retrieving the collection of objects from the REST Web service. This collection is composed of fake photos, each of which has a title and a bitmap (single color image). At this stage, the app only shows one button with the caption Get list. This button is disabled until the data is downloaded, as shown in the first row of Figure 1.
Tap the button and an action sheet (second row of Figure 1) appears displaying an alert that’s composed of several buttons, defined as actions or action buttons (bit.ly/2EEUZpL). In this example, the action sheet provides the action buttons, whose captions con- tain the range of photos to be displayed. When you tap an action, the selected photos are displayed in the table control (bit.ly/2Caq0nM) just below the Get list button as shown in the last row in Figure 1. This table is scrollable, so you can scroll down the list to see all photos in the group.
I’ll implement the communication with the Web service in a separate .NET Standard Class Library (bit.ly/2HArfMq). Following the referenced documentation, .NET Standard is a formal specifica- tion of the .NET APIs designed to accomplish uniform access to programming interfaces available on all .NET implementations. One of the main advantages of this approach is that a shared
This article discusses:
• Develop apps for Apple Watch with Xamarin and Visual Studio for Mac
• Learn how to reuse your existing C# code for wearables through .NET Standard
• Discover .NET Standard Class Libraries for code sharing Technologies discussed:
Xamarin.iOS, Visual Studio for Mac, .NET Standard
42 msdn magazine


































































































   46   47   48   49   50