jueves, 8 de diciembre de 2016

Labels for text - Xamarin



Let’s create a new Xamarin.Forms PCL solution, named Greetings, using the same process described above for creating the Hello solution. This new solution will be structured more like a typical Xamarin.Forms program, which means that it will define a new class that derives from ContentPage. Most of the time in this book, every class and structure defined by a program will get its own file. This means that a new file must be added to the Greetings project:

In Visual Studio, you can right-click the Greetings project in the Solution Explorer and select Add > New Item from the menu. At the left of the Add New Item dialog, select Visual C# and Cross- Platform, and in the center area, select Forms ContentPage. (Watch out: There’s also a Forms ContentView option. Don’t pick that one!)

In Xamarin Studio, from the tool icon on the Greetings project, select Add > New File from the menu. In the left of the New File dialog, select Forms, and in the central area, select Forms ContentPage. (Watch out: There are also Forms ContentView and Forms ContentPage Xaml op-tions. Don’t pick those!)

In either case, give the new file a name of GreetingsPage.cs.

The GreetingsPage.cs file will be initialized with some skeleton code for a class named Greet-ingsPage that derives from ContentPage. Because ContentPage is in the Xamarin.Forms namespace, a using directive includes that namespace. The class is defined as public, but it need not be because it won’t be directly accessed from outside the Greetings project.

Let’s delete all the code in the GreetingsPage constructor and most of the using directives, so the file looks something like this:

using System;
using Xamarin.Forms;
namespace Greetings
{
public class GreetingsPage : ContentPage
{
public GreetingsPage()
{
}
}
}

In the constructor of the GreetingsPage class, instantiate a Label view, set its Text property, and set that Label instance to the Content property that GreetingsPage inherits from ContentPage:

using System;
using Xamarin.Forms;
namespace Greetings
{
public class GreetingsPage : ContentPage
{
public GreetingsPage()
{
Label label = new Label();
label.Text = "Greetings, Xamarin.Forms!";
this.Content = label;
}
}
}

Now change the App class in App.cs to set the MainPage property to an instance of this Greet-ingsPage class:

using System;
using Xamarin.Forms;
namespace Greetings
{
public class App : Application
{
public App()
{
MainPage = new GreetingsPage();
}
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
}
}
}

It’s easy to forget this step, and you’ll be puzzled that your program seems to completely ignore your page class and still says "Welcome to Xamarin Forms!"

It is in the GreetingsPage class (and others like it) where you’ll be spending most of your time in early Xamarin.Forms programming. For some single-page, UI-intensive programs, this class might con-tain the only application code that you’ll need to write. Of course, you can add additional classes to the project if you need them.

In many of the single-page sample programs in this book, the class that derives from ContentPage will have a name that is the same as the application but with Page appended. That naming convention should help you identify the code listings in this book from just the class or constructor name without seeing the entire file. In most cases, the code snippets in the pages of this book won’t include the us-ing directives or the namespace definition.

Many Xamarin.Forms programmers prefer to use the C# 3.0 style of object creation and property initialization in their page constructors. You can do this for the Label object. Following the Label constructor, a pair of curly braces enclose one or more property settings separated by commas. Here’s an alternative (but functionally equivalent) GreetingsPage definition:

public class GreetingsPage : ContentPage
{
public GreetingsPage()
{
Label label = new Label
{
Text = "Greetings, Xamarin.Forms!"
};
this.Content = label;
}
}

This style of property initialization allows the Label instance to be set to the Content property di-rectly, so that the Label doesn’t require a name, like so:

public class GreetingsPage : ContentPage
{
public GreetingsPage()
{
Content = new Label
{
Text = "Greetings, Xamarin.Forms!"
};
}
}

For more complex page layouts, this style of instantiation and initialization provides a better visual analogue of the organization of layouts and views on the page. However, it’s not always as simple as this example might indicate if you need to call methods on these objects or set event handlers.

Whichever way you do it, if you can successfully compile and run the program on the iOS, Android, and Windows 10 Mobile platforms on either an emulator or a device, here’s what you’ll see:


The most disappointing version of this Greetings program is definitely the iPhone: Beginning in iOS 7, a single-page application shares the screen with the status bar at the top. Anything the application displays at the top of its page will occupy the same space as the status bar unless the application com-pensates for it.

This problem disappears in multipage-navigation applications discussed later in this book, but until that time, here are four ways (or five ways if you’re using an SAP) to solve this problem right away.


I hope I have helped in something. Until the next opportunity!











  

No hay comentarios:

Publicar un comentario en la entrada