Tizen .NET App in Just 5 Steps

Tizen .NET App in Just 5 Steps

BY Reni Mathew 28 Nov 2018 Tizen .NET Application, Tizen Studio

With the advancement of technology in various domains, smart devices are proliferating in the tech ecosphere. The demand for smart device applications is also growing exponentially. Thus, it becomes imperative that the app developers develop apps at a rapid pace, while ensuring the quality and usability of the app.

Have you ever thought about creating an app?

In app development, native app development is very popular and has many advantages of its own; however, the cross-platform app development is also gaining popularity. The cross-platform app development is usually cost effective, easy to deploy, and supports code reusability. It targets the smart technical geeks, wherein they can create an app design and implement it on multiple platforms. As per the analysis, cross-platform app market is expected to reach $7.5 billion by the end of 2018.

Microsoft Visual Studio is an IDE that supports cross-platform app development with .NET architecture. In Visual Studio, you can create one app that will run on multiple platforms such as Tizen, Android, IOS, and so on. All you need is a single UI and different platform specific implementation.

Tizen is a very flexible and powerful Linux-based platform for app development. Tizen platform supports many programming languages such as, C, C++, HTML, JavaScript, and C# (using Visual Studio).

The amalgamation of Tizen and Xamarin concepts in Visual Studio is considered something like a cherry on the cake. If an app developer can get hands on with it, expect magic with this blend.

If I say, anybody can create an app, will you agree with me?

By the end of this blog post, you will get your answer.

This blog post will help you to create a Tizen .NET cross-platform app in just five steps. You can use the same UI for other platforms by including the platform specific implementations in your solution.

Before getting started, I suggest that you go through:

If you are done with the basics, ensure you set up the development environment to create an app. For development environment, you need:

  1. Visual Studio 2017 and minimum 1.5 GB of available disk space.
  2. Java Development Kit (JDK) 8.
  3. Visual Studio tools for Tizen.
  4. Tizen Baseline SDK.
  5. Emulator Manager (latest version).

If you are done with the setup, let us try developing a cross platform app in Visual Studio for Tizen. Let us begin with creating a simple app to check the battery percentage, level, and status. To create this app you need to make use of the Tizen. Net Battery API, which is a part of the Tizen.System.

The app uses Battery API to get:

  • Current battery percentage
  • Battery level
  • Battery status

Envision that the UI should look like this:

 

 

 

 

 

Let us see how to create the app in just five steps:

  1. Create a .NET wearable project.
  2. Design your UI using Xamarin forms.
  3. Define the event handlers and create an interface.
  4. Implement DependencyService.
  5. Build and launch the app.

You can use the same UI design for other platforms such as Android, IOS, and so on. All you have to do is write the event handlers and implementation specific for the respective platform.

In this blog post, Tizen specific implementation is explained.

Step 1: Create a Tizen .NET Wearable project.

In Visual Studio, navigate to File > New > Project. On the new window, select Installed > Visual C# items > Tizen.

Click Tizen App (Xamarin.Forms) template, and select the Wearable profile. Name the project as BatteryApp.

In your Solution Explorer, you will see two different projects and two class files:

  1. BatteryApp, the portable or shared project.
  2. BatteryApp.Tizen.Wearable, the platform specific project.

 

 

 

 

 

 

 

 

 

Once you start developing the app, you need to add more files to the projects.

First, you need a UI for your app.

Step 2: Design your UI using Xamarin forms.

For UI design, add a new XAML page in your BatteryApp project. In Solution Explorer, right click BatteryApp > Add > New Item. Select Installed > Visual C# items > Xamarin.Forms > Content Page. Name it as MainPage.xaml. This is the main page that you see when your app is launched.

Now, add the following user controls:

Button Description
Percentage Checks and displays the battery charge (in percent).
Battery Level Checks and displays whether or not the device needs attention.
Battery Status Checks and displays the status of the battery.
Reset Reset the display.

In addition, you need three labels to display the output as shown in the following figure:

 

 

 

 

 

 

 

 

 

 

 

You have four button controls, and you need to create Clicked event handlers for each button. The events are defined in the code-behind page. The code-behind page for MainPage.xaml is MainPage.xaml.cs, which is automatically generated. You can see it in your Solution Explorer.

Step 3: Define the event handlers and create an interface.

In MainPage.xaml.cs, you need to define the Clicked event handlers for each button as:

        public void Onbuttonpercent(object sender, EventArgs e)
        {
            
        }

        public void Onbuttonlevel(object sender, EventArgs e)
        {
            
        }

        public void Onbuttonstatus(object sender, EventArgs e)
        {
            
        }

        public void Onreset(object sender, EventArgs e)
        {
            this.textPercentage.Text = "";
            this.textLevel.Text = "";
            this.textStatus.Text = "";
        }        

The functionality of the Percentage button is to display the remaining battery percentage. For this, you need to use the Battery API from Tizen.System. To enable the methods and properties of the Tizen.System namespace classes, you need to add this namespace in your application. This namespace is added to the platform specific project, BatteryApp.Tizen.Wearable.

In order to use it in the clicked events written in MainPage.xaml.cs, apply the concept of DependencyService in Xamarin using an interface. This interface is used to communicate between portable and platform specific projects.

NOTE: For some APIs, you must add the privileges in the Tizen manifest file, tizen-manifest.xml. In this example, you do not need to add. However, if you select any other API, ensure that you read the specifications and add the appropriate privileges.

Now, you need to add an interface in the portable or shared project, BatteryApp. From Solution Explorer, right click BatteryApp > Add > New Item. Select Installed > Visual C# items > Interface. Name the file as Ibattery.cs.

Now, declare and define an interface Ibattery in Ibattery.cs file:

using System;
using System.Collections.Generic;
using System.Text;

namespace BatteryApp
{
    public interface Ibattery
    {
       int RemainingChargePercent { get; }
       string BatteryLevel { get; }
       string BatteryStatus { get; }
    }
}

Step 4: Implement DependencyService.

Now, you have the UI and an interface for communication. You need to write the implementation.

From the Solution Explorer, right click BatteryApp > Add > New Item. Select Installed > Visual C# items > Class and name it as BatteryImpl.cs.

  1. Implement the interface in the platform specific project’s class file, BatteryImpl.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BatteryApp.Tizen.Wearable;
    using Tizen.System;

    // Register the dependency service by adding the following: 
    [assembly: Xamarin.Forms.Dependency(typeof(BatteryImpl))]

    namespace BatteryApp.Tizen.Wearable
    {
        public class BatteryImpl:Ibattery
        {
        // Get the remaining charge percentage using Tizen.System.Battery class.

        public int RemainingChargePercent
        {
            get
            {
                return (int)(Battery.Percent);
            }

        }

        //Get the device battery level using   Tizen.System.BatteryLevelStatus enumeration.

        public string BatteryLevel
        {
            get
            {
                BatteryLevelStatus status;
                status = Battery.Level;
                return (string)(status.ToString());
            }
        }

        // Get the current battery charging state using the IsCharging property. 
        public string BatteryStatus
        {
            get
            {
                bool charging;
                charging = Battery.IsCharging;
                return (string)(charging.ToString());
            }
        }
    }
}
    

 2. In MainPage.xaml.cs file, use the DependencyService.Get method with the interface to get the value from platform specific project:

using System;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace BatteryApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class MainPage : ContentPage
	{
		public MainPage ()
		{
			InitializeComponent ();
		}

        public void Onbuttonpercent(object sender, EventArgs e)
        {
            // Using DependencyService.Get method with the interface getting the percentage from Tizen.System.Battery class.
            var bat = DependencyService.Get<Ibattery>();   
            this.textPercentage.Text = bat.RemainingChargePercent.ToString();
        }

        public void Onbuttonlevel(object sender, EventArgs e)
        {
            // Using DependencyService.Get method with the interface getting the device battery level using Tizen.System.BatteryLevelStatus enumeration.
            var bat = DependencyService.Get<Ibattery>();
            this.textLevel.Text = bat.BatteryLevel;
        }

        public void Onbuttonstatus(object sender, EventArgs e)
        {
            // Using DependencyService.Get method with the interface getting the current battery charging state using the IsCharging property. 
            var bat = DependencyService.Get<Ibattery>();
            bool status = Convert.ToBoolean(bat.BatteryStatus);
            if (status == true)
            {
                this.textStatus.Text = "Is charging";
            }
            else
            {
                this.textStatus.Text = "Not charging";
            }
        }

        public void Onreset(object sender, EventArgs e)
        {
            // Clear the labels.
            this.textPercentage.Text = "";
            this.textLevel.Text = "";
            this.textStatus.Text = "";
        }
    }
}

Step 5: Build and launch the app.

Finally, you are done with the coding part. Now, build the project.

In Solution Explorer, right click on the solution, BatteryApp and select Build Solution:

 

 

 

 

 

 

 

 

 

Click Launch Tizen Emulator in the tool bar to launch the Emulator Manager. Here, you can select the type of emulator. Since you are creating Wearable app, select Wearable Circle:

   

 

 

 

 

 

 

 

 

       

  

Launch the emulator and see the output on your emulator:

 

 

 

 

 

 

 

 

 

 

 

 

In just five steps, you created a simple Tizen .NET cross platform app. 

You can select any Tizen .NET API and create your desired application. If you need more ideas or you want to practice, take a dive in the treasure full of sample apps.

I think now you will agree with me that anybody can create a Tizen .NET cross-platform app in just five steps. Various Tizen .NET APIs are available to make this task even better. So, what are you waiting for? Go explore, try app development, and be in demand.

File attachments: 
Written by Reni Mathew