This post was originally published at Mixing and Matching UIKit with XAML and the Windows Bridge for iOS

UIKit_XAML_Hero
In part one of the Windows Bridge for iOS series, we created a simple to-do list app in Xcode and used the Windows Bridge for iOS to bring it over to Windows 10. In part two of the series, we went on a tour of Visual Studio for iOS developers. In part three, we used the Windows Bridge for iOS to convert an iOS calculator app created using Storyboards and Auto Layout to a Universal Windows Platform app that adjusts to various form factors on Windows 10.

Today, we discuss the various options that the Windows Bridge for iOS provides developers with, including controls in their converted UWP apps–whether UIKit, XAML, or some combination of both.

Getting Started

For today’s tutorial, you will need:

A Note on XAML Interoperability

If you’ve been following the bridge project, you know that UIKit and XAML interoperability has been an important goal from the outset. The bridge uses the XAML compositor to manage your UIKit views and perform animations on them by linking CALayers to XAML elements. This approach, combined with our Objective-C® projections of Windows APIs, allows for some really cool stuff, like mixing and matching UIKit and XAML controls in the same view hierarchy with ease.

It’s important to note that whether you decide to use XAML components in Objective-C or stick with UIKit, you’re still creating native Windows interfaces. With the release of Windows 10, however, the implementation of various UIKit controls was looking a bit dated:

1_UIKitcontrolsdated

In the latest releases of the bridge, that same screen now looks like this:

2_UIKitcontrolsupdated

We matched iOS controls to their native Windows counterparts, so whether you use a UIKit UISwitch or a XAML ToggleSwitch, your control will look right at home on Windows 10.

These new controls are a work in progress, and for the time being, they rely heavily on bitmaps and thus won’t respond to customizations. (For instance, setting the background color on a UISwitch won’t work.) But we’re working on translating the designs into vector graphics so that in the future they’ll be fully polished and customizable.

Diving In

We are going to walk through a simple coded user interface example. We’ll first create an iOS app, then convert it to a UWP app, and finally, swap out the UIKit controls for XAML controls.

In Xcode, create a new Single View Application project and name it MixMatchUIKitXaml.

3_singleviewapplication

Pick out a good location to store your project files and then click “Create.” Xcode will do what it does and load up the blank Single View Application template and project structure as previously discussed.

The app we are going to create is going to contain a UIButton, a UISwitch, and a UISlider. Clicking the button, flipping the switch, or moving the slider will update a corresponding UILabel.

In ViewController.h, let’s define three UILabel variables.

4_viewcontroller.h

Next open ViewController.m, and let’s add a helper function for creating our UILabels.

In the viewDidLoad method, let’s allocate and initialize our UIElements.

Finally, let’s add our actionMethods to enable the button, switch and slider to update their corresponding labels.

With everything completed, your ViewController.m file and project should look like the following:

5_viewcontroller.m

Perfect! Now, let’s build and then run the app in the Simulator and confirm that everything is working as expected.

6_simulator

Since we already have the Xcode project open, let’s add in the code for telling Windows how to scale responsively as discussed in more detail here and here. To do so, open your AppDelegate.m file and paste in the following code. This snippet uses the #ifdef and #endif preprocessor directives to check to see if the WINOBJC symbol is defined, giving us the ability to include Windows-specific code that will be ignored by Xcode and when we run the app on iOS.

Great! Now save the Xcode project and copy the files over to your Windows machine.

Note: If you don’t have a PC, you can download one of the pre-built evaluation virtual machines from the Windows Bridge for iOS website. Download the package for your preferred virtualization environment and you’ll be up and running in no time. The package already includes Windows 10, Visual Studio 2015, and the iOS bridge.

Converting the Project

In previous posts, we’ve gone into detail on how to use the Windows Bridge for iOS and how it works, so we aren’t going to delve into that this time. Just run the MixMatchUIKitXaml Xcode project through the vsimporter tool and then open up the Visual Studio solution it created. When the solution opens, run the solution in the Simulator. Notice how everything is still functioning as expected.

7_convertingtheproject

Swapping Out Controls

Using the Windows Bridge for iOS, we can use UIKit and XAML controls interchangeably. To demonstrate, let’s swap out the UIButton with a XAML button. The first step is to add the following reference to your ViewController.h file.

Then, in ViewController.m, we’ll comment out the code for adding the UI Button and the buttonAction method and add in the code for adding the WXCButton, which is the iOS Bridge implementation of the XAML button. You’ll notice a slight difference in code needed to both allocate and initialize the control, add the event handler, and then add it to the main view, but the basic syntax is pretty intuitive.

With the changes in place, let’s run the app again.

8_allocateinitialize

We now have a XAML button running inside our app, updating a UIKit Label control. Let’s quickly swap out the UISwitch and Slider controls as well.

Running the app, you’ll see that the XAML controls are now taking the place of the UIKit controls and are actively updating the existing UIKit labels. We have also added values to the “offContent” and “onContent” properties of the WXCToggleSwitch to demonstrate the capability to add content directly there in addition to being able to fire an event when toggled.

9_XAMLcontrols

Adding Windows 10 Functionality to your App with a Live Tile

What is a UWP app without its very own Live Tile? Let’s add one. In ViewController.h, add the following references.

Now, in ViewController.m, let’s add a little code. Add in an #ifdef section (so the code will only execute in Windows) in the viewDidLoad method, we add a WUNTileUpdater (Windows.UI.Notifications.TileUpdater). Next, we’ll specify a tile template to use (WUNTileTemplateTypeTileWide310x150Text03) and then add some text to display. Finally, we’ll create a tile notification and tell the tileUpdater to update. The code should appear as follows:

Run the app, open your start menu, and find the MixMatchUIKitXaml app. Right-click and select “Pin to Start.”

10_pintostart

A “Medium” tile will be added by default. Our code is specific to the wide tile, so right-click your tile and select the “Wide” option.

11_mediumtile

And there you have it, your very own Live Tile for your converted iOS app.

12_finishedtile

Wrapping Up

In this post, we created a coded UI iOS app and then demonstrated how easy it is to swap out the UIKit controls for their XAML counterparts. We also demonstrated interoperability by showing how the XAML controls are to interact with UIKit controls within the app.

Lastly, we’d like to share a new resource immediately available to iOS and Android developers. This document shows how to map fundamental programming concepts that you are already familiar with to Windows, and drawing out the relationship between each pair. It will allow you to quickly take your existing knowledge and apply it to new Windows apps!

Additional Information can be found here so be sure to check out the first three posts in the series linked above:

Link – 

This post was originally published at Mixing and Matching UIKit with XAML and the Windows Bridge for iOS