Updating Mobile Development Environment

On 20th July, Microsoft released Visual Studio 2015. This release of Visual Studio includes improvements in terms of IDE features, Language features and even changes in underlying .NET framework. Along with this, Microsoft is also releasing Windows 10 on 29th July, which will add Universal Windows Platform (UWP) development capabilities to Visual Studio. In this blog post, I’m going to cover the features that are most important for us i.e. mobile developers. Having said that, if you want to build UWP apps on RTM, hold on till July 29th, as the SDK and tooling for UWP will be released on the same day. You can find that information here.

Install Selection

You can think Visual Studio 2015 as the most complete mobile development IDE. You can build any kind of mobile applications which can run on Android, iOS and Windows devices. To setup your environment for mobile development, while installing itself, make sure you select your required tools. For example, here I’ve selected Xamarin tools to build native mobile applications. In this case, Visual Studio will install the necessary SDKs and libraries e.g. Android SDKs & NDKs with different API levels, Java SDK, etc…

VS 2015 Install

If you’re already a Xamarin developer, you may want to install Xamarin Studio, which needs to be installed separately by downloading Xamarin Installer from the site. With open sourcing of .NET and Roslyn, even Xamarin Studio is using some of its benefits. You can read more about it from Miguel’s blog.

Xamarin Integration & Project Template

Previously (VS 2013 or prior), you had to install Xamarin to get Xamarin specific project templates. With VS2015, if you select above option during install, you’ll have Xamarin project templates in File > New Project Dialog.

VS 2015 Project Templates

Note: The difference between Blank App (Native Portable) & Blank App (Xamarin.Forms Portable) is literal. That means, later one has Xamarin.Forms NuGet and integration in all projects. First one, just creates 4 separate projects without Xamarin.Forms dependencies.

When you create a Xamarin project, Visual Studio will ask you to sign into your Xamarin account to use different features according to license.

VS 2015 Xamarin

Developer (aka God) Mode

As I mentioned above, if you want to build UWP apps on RTM, you’ll need to wait till 29th July. And to enable Windows Store apps development and debugging on your machine, you’ll need to enable a ‘Developer Mode’. When you create a new project you’ll get following dialog which can take you to next dialog, where you need to select ‘Developer Mode’ and you should be able to test your apps. Do the same thing in your Windows 10 mobile device, if you want to debug apps directly on mobile.

VS 2015 - Developer Mode

VS 2015 - Developer Mode Enabled

Accessing Tools

This is not new as such to Visual Studio 2015, but I want to add it here for your reference. You can find all the tools and setting related to Xamarin and mobility under Tools menu. So, if you want to download new API Level (for example Android M Preview) or monitor your Android app’s performance on device using Android Device Monitor, you know where to look for. We’ll cover tools and features exclusive for Windows 10 in next post.

VS 2015 Tools

By the way, if you ever face any issues with Xamarin and need to contact support, Xamarin logs can help you to share more information about your issues with Support guys. Even, you can go through them and find issues/reasons. You can find them easily under Help > Xamarin

If you want to check where exactly platform SDKs are installed, you can find in Tools > Options and at two different selections

VS 2015 - SDK Location - 1

VS 2015 - SDK Location - 2

More Resources

Now, if you want to know more about Visual Studio 2015 and .NET Framework, Microsoft Channel 9 has Connect On-Demand series on it. Some of the most important videos are here

What’s new in C# 6
New Coding Experiences for C# and Visual Basic
What’s New for .NET 2015
Developing iOS and Android Apps in C# with Visual Studio
Xamarin.Forms: Leaveraging XAML to Build iOS, Android, and Windows Apps
Wearables in C#: Highlighting Apple Watch, Android Wear, and Microsoft Band
GitHub Extensions for Visual Studio

I’ll update tendulkar-uvāca series with new tools and technologies once they are released i.e. after 29th July. Till then, download the Visual Studio 2015 and enjoy the new IDE & tooling.

By the way, have I told you: You get Visual Studio 2015 Community Edition free for specific use cases and along with it, you get Xamarin Starter for free. If you’re a student, you can get more benefit through Microsoft DreamSpark program.

Namaste
Mayur Tendulkar

 

Plugin 05: Accelerometer

In our series on plugins, today we’re going to cover plugin which can help us to work with Accelerometer sensor. This sensor is responsible to detect X, Y, Z co-ordinates of the devices. Using data received from this sensor, you can build games like TempleRun, where users can collect the coins by tilting the device.

There are different APIs for different platform and their usage is different too. First, lets see how we can use this sensor in different platform and then we’ll use plugin in Xamarin.Forms application.

Being Windows Runtime API, the API and its usage is same in Windows and Windows Phone. Here, you create object of the sensor and register for ‘ReadingChanged’ event. Whenever device position is changed, it is reported through that event and UI can be updated using Dispatcher.

public sealed partial class MainPage : Page
{
private Accelerometer _accelerometer;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
// Establish the report interval
uint minReportInterval = _accelerometer.MinimumReportInterval;
var desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
_accelerometer.ReportInterval = desiredReportInterval;
try
{
_accelerometer.ReadingChanged += async (a, b) =>
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
txtXAxis.Text = "X axis: " + b.Reading.AccelerationX.ToString("F");
txtYAxis.Text = "Y axis: " + b.Reading.AccelerationY.ToString("F");
txtZAxis.Text = "Z axis: " + b.Reading.AccelerationZ.ToString("F");
});
};
}
catch (Exception ex)
{
throw;
}
}
else
{
new Windows.UI.Popups.MessageDialog("Accelerometer not found", "Error:").ShowAsync();
}
}
}

In case of Android, it is again similar to Windows platform. Create object of SensorManager and register for SensorChanged event.

[Activity(Label = "Accelerometer_Droid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity, ISensorEventListener
{
private SensorManager _sensorManager;
private static readonly object _syncLock = new object();
private TextView x_axis; private TextView y_axis; private TextView z_axis;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
_sensorManager = (SensorManager)GetSystemService(Context.SensorService);
x_axis = FindViewById<TextView>(Resource.Id.lblXAxis);
y_axis = FindViewById<TextView>(Resource.Id.lblYAxis);
z_axis = FindViewById<TextView>(Resource.Id.lblZAxis);
}
public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
{
//Doing nothing. Just implementing interface.
}
public void OnSensorChanged(SensorEvent e)
{
lock (_syncLock)
{
x_axis.Text = e.Values[0].ToString("F");
y_axis.Text = e.Values[1].ToString("F");
z_axis.Text = e.Values[2].ToString("F");
}
}
protected override void OnResume()
{
base.OnResume();
_sensorManager.RegisterListener(this, _sensorManager.GetDefaultSensor(SensorType.Accelerometer), SensorDelay.Ui);
}
protected override void OnPause()
{
base.OnPause();
_sensorManager.UnregisterListener(this);
}
}

iOS is not any different. You create object of CMMotionManager and then listen to sensor for changes.

public override void ViewDidLoad()
{
base.ViewDidLoad();
_motionManager = new CMMotionManager();
_motionManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, (data, error) =>
{
lblXAxis.Text = data.Acceleration.X.ToString("F");
lblYAxis.Text = data.Acceleration.Y.ToString("F");
lblZAxis.Text = data.Acceleration.Z.ToString("F");
});
// Perform any additional setup after loading the view, typically from a nib.
}

As you can see, for three different platforms the APIs are totally different. But with Plugins for Xamarin.Forms it becomes easy. You can use DeviceMotion plugin which can work across all these platforms. And the code for same looks like:

private void BtnStart_OnClicked(object sender, EventArgs e)
{
CrossDeviceMotion.Current.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui);
CrossDeviceMotion.Current.SensorValueChanged += (s, a) =>
{
switch (a.SensorType)
{
case MotionSensorType.Accelerometer:
lblXAxis.Text = ((MotionVector)a.Value).X.ToString("F");
lblYAxis.Text = ((MotionVector)a.Value).Y.ToString("F");
lblZAxis.Text = ((MotionVector)a.Value).Z.ToString("F");
break;
}
};
}
private void BtnStop_OnClicked(object sender, EventArgs e)
{
CrossDeviceMotion.Current.Stop(MotionSensorType.Accelerometer);
}

In this series on plugin we’ve seen how particular concept can be implemented in all the three platforms and how we can use plugin to make life easier. This will be be last post in this series, but there are many plugins available which you can explore and use in your projects. Do let me know if you want to cover any specific plugin. We’ll start with a new series in couple of weeks. Till then… happy coding :)

Namaste
Mayur Tendulkar

Don’t Re-Invent the Wheel: Use Components or Plugins In Your Apps

Xamarin provides native, cross-platform mobile application development using language we all know and love – C#. However, it is still required to learn concepts of each platform. For example to send text message (SMS) in all these three platforms there are different set of APIs.

Below API is for Android, which uses SmsManager to send the text message. There is another way to do this using Intents.

image 

In case of Windows Phone, ChatMessage allows us to declare and send text message. This API is available in WinRT platform. In case of SL based apps, we need to use Launchers/Choosers.

image

In case of iOS as shown below, there is no API as such. But we use URL to launch default messaging app. In this case, we cannot set the message body, which we want to send out.

image

Life will be much simpler if there will be just one API on all these platforms which will take care of sending text messages and as I developer we don’t need to bother about on which platform it is being called. Here comes components and plugins. My friend James has written a nice blog about ‘What Exactly is a Plugin for Xamarin?’. You can read it here

In our case, we’re going to use Messaging Plugin for Xamarin and Windows which allow us to use same API to send text messages. Below code is written once in a Xamarin.Forms project which depending on platform on which code is executing, send the message using that platform’s APIs.

image

Using this plugin, it becomes easier to send text messages on Android, iOS and Windows. And this is the magic of Plugins for Xamarin.

In future posts, we’ll cover some of the best plugins and components which can help you build cross-platform mobile applications with single code-base.

I hope you’ve enjoyed this.

Namaste
Mayur Tendulkar