This topic describes how to implement basic functionality in your .NET application, using Xamarin.Forms.
Most Tizen .NET TV applications have a UI. The Tizen .NET TV framework supports the following application UI models:
The following sections describe how to implement basic application functionality using Xamarin.Forms.
You can implement code that is run at various stages of the application life-cycle.
In the <projectname> (Portable) project, the <projectname>.cs file contains 3 virtual methods that can be overridden to handle application state change events:
<projectname> (Portable)
<projectname>.cs
OnStart()
OnSleep()
OnResume()
public class App : Application { protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } }
In the <projectname>.Tizen.TV project, the <projectname>.Tizen.TV.cs file contains 5 virtual methods that can be overridden to handle application state change events:
<projectname>.Tizen.TV
<projectname>.Tizen.TV.cs
OnCreate()
Run()
OnAppControlReceived()
OnPause()
OnTerminate()
For more information on the application life-cycle and state change events, see Application Management.
You must call the base class method within the override. Otherwise, events are not emitted.
using Tizen.Applications; namespace XamarinApp.Tizen.TV { class Program : global::Xamarin.Forms.Platform.Tizen.FormsApplication { protected override void OnCreate() { base.OnCreate(); LoadApplication(new App()); } protected override void OnAppControlReceived(AppControlReceivedEventArgs e) { base.OnAppControlReceived(e); } protected override void OnPause() { base.OnPause(); } protected override void OnResume() { base.OnResume(); } protected override void OnTerminate() { base.OnTerminate(); } } }
The Xamarin.Forms controls used to create the user interface of a Tizen .NET application can be broadly categorized into 4 groups:
INavigation
ContentPage
StackLayout
For more information on Xamarin.Forms, see the Xamarin Developer Center. There is also a comprehensive book about Xamarin.Forms available as a free download from Microsoft Press.
The following example uses the "Blank App (Tizen Xamarin.Forms Portable)" template as a basis. It shows you how to modify the existing code to add a button and a label that changes when the button is clicked. For more information on creating, building, and running the template application, see Creating .NET TV Applications.
The "Blank App" application uses a label which displays text in an area of the screen. The properties inherited from the base classes of Label give control over the display: font attributes, families, and sizes, as well as layout options. Modify the application by adding a button control. It is similar to the label, but is specifically designed to react to click events. As a result, the Button class defines the Clicked event, which tells the application what to do when the click event takes place.
Label
Button
Clicked
In order to do something visible on the screen to show that you have received the click event, define another label. Give the new label a value to be displayed in the initial state, and make the button click event handler update the text and button color once the click event triggers.
To modify the application by adding a button and label:
OnButtonClicked()
Text
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Xamarin.Forms; namespace XamarinApp { public class App : Application { Label label; Button button; int clicks = 0; public App() { button = new Button { Text = "Click here!", BackgroundColor = Color.Red, HorizontalOptions = LayoutOptions.Center, }; button.Clicked += OnButtonClicked; label = new Label { Text = "unclicked", HorizontalOptions = LayoutOptions.Center, }; // The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "Welcome to Xamarin Forms!" }, button, label } } }; } void OnButtonClicked(object s, EventArgs e) { clicks += 1; label.Text = String.Format("Number of clicks: {0}", clicks); button.BackgroundColor = Color.White; } protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } } }
The following figures show what happens when you run the modified code. At startup, the button is red and the text below the button is "unclicked". After a couple of clicks, the click counter is displayed below the button, and the button color has changed.
To add resources, such as images and audio files, to the TV application, place them in the "res" directory of the "Tizen.TV" project.
The resources are visible in the Visual Studio "Solution Explorer" view, and are included in the ".tpk" file when the project is built.
To implement an image in the application, create an Image instance and set the file path within the "res" directory as the source:
Image
public class App : Application { Image image = null; public App() { image = new Image { Source = "img_dist_1_01.png" }; MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { image, } } }; } }