sábado, 26 de noviembre de 2016

Say hello - Xamarin



The modern user interface is constructed from visual objects of various sorts. Depending on the oper-ating system, these visual objects might go by different names—controls, elements, views, widgets—but they are all devoted to the jobs of presentation or interaction or both.

In Xamarin.Forms, the objects that appear on the screen are collectively called visual elements. They come in three main categories:
  • page
  • layout
  • view
These are not abstract concepts! The Xamarin.Forms application programming interface (API) de-fines classes named VisualElement, Page, Layout, and View. These classes and their descendants form the backbone of the Xamarin.Forms user interface. VisualElement is an exceptionally important class in Xamarin.Forms. A VisualElement object is anything that occupies space on the screen.

A Xamarin.Forms application consists of one or more pages. A page usually occupies all (or at least a large area) of the screen. Some applications consist of only a single page, while others allow navi-gating between multiple pages. In many of the early chapters in this book, you’ll see just one type of page, called a ContentPage.

On each page, the visual elements are organized in a parent-child hierarchy. The child of a Con-tentPage is generally a layout of some sort to organize the visuals. Some layouts have a single child, but many layouts have multiple children that the layout arranges within itself. These children can be other layouts or views. Different types of layouts arrange children in a stack, in a two-dimensional grid, or in a more freeform manner. In this chapter, however, our pages will contain just a single child.

The term view in Xamarin.Forms denotes familiar types of presentation and interactive objects: text, bitmaps, buttons, text-entry fields, sliders, switches, progress bars, date and time pickers, and others of your own devising. These are often called controls or widgets in other programming environments. This book refers to them as views or elements. In this chapter, you’ll encounter the Label view for dis-playing text.


Say hello

Using either Microsoft Visual Studio or Xamarin Studio, let’s create a new Xamarin.Forms application by using a standard template. This process creates a solution that contains up to six projects: five platform projects—for iOS, Android, the Universal Windows Platform (UWP), Windows 8.1, and Windows Phone 8.1—and a common project for the greater part of your application code.

In Visual Studio, select the menu option File > New > Project. At the left of the New Project dia-log, select Visual C# and then Cross-Platform. In the center part of the dialog you’ll see several avail-able solution templates, including three for Xamarin.Forms:
  • Blank App (Xamarin.Forms Portable)
  • Blank App (Xamarin.Forms Shared)
  • Class Library (Xamarin.Forms)
Now what? We definitely want to create a Blank App solution, but what kind?

Xamarin Studio presents a similar dilemma but in a different way. To create a new Xamarin.Forms solution in Xamarin Studio, select File > New > Solution from the menu, and at the left of the New Project dialog, under Multiplatform select App, pick Forms App, and press the Next button. Toward the bottom of the next screen are a pair of radio buttons labeled Shared Code. These buttons allow you to choose one of the following options:
  • Use Portable Class Library
  • Use Shared Library
The term “Portable” in this context refers to a Portable Class Library (PCL). All the common applica-tion code becomes a dynamic-link library (DLL) that is referenced by all the individual platform pro-jects.

The term “Shared” in this context means a Shared Asset Project (SAP) containing loose code files (and perhaps other files) that are shared among the platform projects, essentially becoming part of each platform project.

For now, pick the first one: Blank App (Xamarin.Forms Portable) in Visual Studio or Use Portable Class Library in Xamarin Studio. Give the project a name—for example, Hello—and select a disk loca-tion for it in that dialog (in Visual Studio) or in the dialog that appears after pressing the Next button again in Xamarin Studio.

If you’re running Visual Studio, six projects are created: one common project (the PCL project) and five application projects. For a solution named Hello, these are:
  • A Portable Class Library project named Hello that is referenced by all five application projects;
  • An application project for Android, named Hello.Droid;
  • An application project for iOS, named Hello.iOS;
  • An application project for the Universal Windows Platform of Windows 10 and Windows Mobile 10, named Hello.UWP;
  • An application project for Windows 8.1, named Hello.Windows; and
  • An application project for Windows Phone 8.1, named Hello.WinPhone.
If you’re running Xamarin Studio on the Mac, the Windows and Windows Phone projects are not created.

When you create a new Xamarin.Forms solution, the Xamarin.Forms libraries (and various support libraries) are automatically downloaded from the NuGet package manager. Visual Studio and Xamarin Studio store these libraries in a directory named packages in the solution directory. However, the par-ticular version of the Xamarin.Forms library that is downloaded is specified within the solution tem-plate, and a newer version might be available.

In Visual Studio, in the Solution Explorer at the far right of the screen, right-click the solution name and select Manage NuGet Packages for Solution. The dialog that appears contains selectable items at the upper left that let you see what NuGet packages are installed in the solution and let you install others. You can also select the Update item to update the Xamarin.Forms library.

In Xamarin.Studio, you can select the tool icon to the right of the solution name in the Solution list and select Update NuGet Packages.

Before continuing, check to be sure that the project configurations are okay. In Visual Studio, select the Build > Configuration Manager menu item. In the Configuration Manager dialog, you’ll see the PCL project and the five application projects. Make sure the Build box is checked for all the projects and the Deploy box is checked for all the application projects (unless the box is grayed out). Take note of the Platform column: If the Hello project is listed, it should be flagged as Any CPU. The Hello.Droid project should also be flagged as Any CPU. (For those two project types, Any CPU is the only option.) For the Hello.iOS project, choose either iPhone or iPhoneSimulator depending on how you’ll be testing the program.

For the Hello.UWP project, the project configuration must be x86 for deploying to the Windows desktop or an on-screen emulator, and ARM for deploying to a phone.

For the Hello.WinPhone project, you can select x86 if you’ll be using an on-screen emulator, ARM if you’ll be deploying to a real phone, or Any CPU for deploying to either. Regardless of your choice, Visual Studio generates the same code.

If a project doesn’t seem to be compiling or deploying in Visual Studio, recheck the settings in the Configuration Manager dialog. Sometimes a different configuration becomes active and might not include the PCL project.

In Xamarin Studio on the Mac, you can switch between deploying to the iPhone and iPhone simula-tor through the Project > Active Configuration menu item.

In Visual Studio, you’ll probably want to display the iOS and Android toolbars. These toolbars let you choose among emulators and devices and allow you to manage the emulators. From the main menu, make sure the View > Toolbars > iOS and View > Toolbars > Android items are checked.

Because the solution contains anywhere from two to six projects, you must designate which pro-gram starts up when you elect to run or debug an application.

In the Solution Explorer of Visual Studio, right-click any of the five application projects and select the Set As StartUp Project item from the menu. You can then select to deploy to either an emulator or a real device. To build and run the program, select the menu item Debug > Start Debugging.

In the Solution list in Xamarin Studio, click the little tool icon that appears to the right of a selected project and select Set As Startup Project from the menu. You can then pick Run > Start Debugging from the main menu.

If all goes well, the skeleton application created by the template will run and you’ll see a short message:


As you can see, these platforms have different color schemes. The iOS and Windows 10 Mobile screens display dark text on a light background, while the Android device displays light text on a black background. By default, the Windows 8.1 and Windows Phone 8.1 platforms are like Android in dis-playing light text on a black background.

By default, all the platforms are enabled for orientation changes. Turn the phone sideways, and you’ll see the text adjust to the new center.

The app is not only run on the device or emulator but deployed. It appears with the other apps on the phone or emulator and can be run from there. If you don’t like the application icon or how the app name displays, you can change that in the individual platform projects.


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











  

No hay comentarios:

Publicar un comentario

       
free counters

Páginas vistas en total según Google