Page 24 - MSDN Magazine, August 2017
P. 24

iOS UIKit
Android SDK
Windows 10 SDK
to the native UI SDKs: iOS UIKit, Android SDK and Windows 10 SDK. You can create platform-specific views and pages in the platform-specific project anytime you need to use functionality in native iOS, Android and Windows.
Using the Xamarin.Forms built-in Dependency Injection, you initialize and reference the custom UI class and Xamarin pulls it out of the appropriate platform’s project for you. That’s how the map page was built in B4UFLY.
But what if you just want to change one or two properties or events and don’t need an entire custom UI class?
Enter Effects. Coding an entire UI renderer class for each platform can be excessive. Sometimes all that’s needed is a tweak to a single control element, such as a drop shadow on a label. While custom renderers expose an entire platform-specific class, Effects exposes just its properties. The entire element needn’t be subclassed, though a platform-specific class is necessary. A Xamarin.Forms effect offers this precision approach to platform-specific UI customization.
What if all you really need is a native control on your Xamarin.Forms layout?
Take the plunge and declare a platform-specific control, some- times called a “native control,” though it’s a Xamarin control and not truly native. Instead of coding overly customized custom renderers, declare native views from Xamarin.iOS, Xamarin.Android or the UWP directly into your Xamarin.Forms layouts. Using a shared project and conditional compilation, include platform-specific UI libraries in your C# UI classes where you can reference them as directly as if you were coding in the native platform. Set properties and event handlers on these views and use them side-by-side with Xamarin.Forms views, in both C# and XAML.
Xamarin.Forms development gives you the ease of cross-plat- form development using C# and a single UI library with a solid
Figure 2 Xamarin Libraries Bind to Native OS Libraries
would then build my UIs with complete access to the underly- ing native SDKs, but I’d need to make UIs for each platform using Xamarin.iOS, Xamarin.Android or Windows 10 SDK.
So when it was first announced that with Xamarin.Forms you could build your mobile UI only once and compile for iOS, Android and the UWP, my heart skipped a beat. That’s because it’s what I always longed for: an end-to-end cross-platform develop- ment experience.
However, I knew just how deep Xamarin already went when it came to native UI and I wondered: “What if I need something that Xamarin.Forms can’t do?”
I asked everyone I knew to explain exactly what Xamarin.Forms could do and what it couldn’t do, and I received many terrific responses that helped me better understand Xamarin.Forms, but no one could really answer my question. So, I wrote a book to answer it: “Xamarin Mobile Application Development” (Apress, 2015). And here’s a spoiler: Use custom renderers.
Custom renderers give you the ability to punch down through the Xamarin.Forms abstraction and gain direct access to Xamarin.Android,Xamarin.iOSandtheUWP.Thismeansaccess
Figure 3 B4UFLY Status Page in a No-Fly Area
Figure 4 Map Page at Lexicon Systems Office in Beverly, Mass.
Figure 5 B4UFLY Planning Mode Page in San Francisco, Calif.
20 msdn magazine
Xamarin.Forms
Xamarin Xamarin.Forms
Xamarin.iOS Xamarin.Android












































































   22   23   24   25   26