在Label中显示一段文字

时间:2021-11-24 10:56:17
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: 

原文

  下面创建一个PCL的Xamarin.Forms解决方案Greetings。在Visual Studio解决方案中,右击Greetings项目,然后选择添加>新建项菜单。在对话框的左侧选择Cross-Platform,右侧选择Froms ContentPage,名称中输入GreetingsPage.cs,然后点击添加。

  添加的GreetingsPage.cs类文件会被一个派生自ContentPage的类GreetingsPage进行初始化,并且包含了已经基本代码。虽然这个类被定义为public,但是它不会在Greetings项目之外被用到。

  下面删除GreetingsPage构造函数中的代码,和大部分using引用,下面是修改结果:

using System;
using Xamarin.Forms; 

namespace Greetings
{
    public class GreetingsPage : ContentPage
    {
        public GreetingsPage()
        {

        }
    }
}

  在GreetingsPage类的构造器中,实例化一个Label视图,并且设置Texts属性,然后将这个label实例化对象赋值给GreetingsPage的Content属性。

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;
        }
    }
}

  下面将App类中的MainPage属性修改成上面的Greetings类的实例化对象。

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

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 contain 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 using 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: 

原文

  许多程序员喜欢在页面构造器中使用C# 3.0的对象创建和属性初始化风格。

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

  如果不需要为Label创建一个对象名称引用,可以直接将Label的实例赋值给Content属性。

  public class GreetingsPage : ContentPage
    {
        public GreetingsPage()
        {
            this.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: 

原文

  对于更复杂的页面布局,这种初始化和实例化的方式,在页面布局和视图的组织上可以提供一个更好的视觉模拟。然而,并不总是那么简单,如果你需要去调用GreetingsPage的方法或者去设置一个事件。

  不管你上面怎么样去实例化label,编译和运行后可以看到下面的效果:

在Label中显示一段文字

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 compensates 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. 

原文

  对于这个Greetings程序来说,运行最让人失望的版本肯定是iPhone:iOS7开始,单页应用程序和顶部的状态栏共享一个屏幕。除非应用程序补偿这个区域,否则应用程序将会占据顶部的状态栏空间。

  在本书后面讨论到多导航应用的时候,这个问题就会消失。但是在那之前,下面提供四种方式来解决这个问题。

Solution 1. Include padding on the page 

The Page class defines a property named Padding that marks an area around the interior perimeter of the page into which content cannot intrude. The Padding property is of type Thickness, a structure that defines four properties named Left, Top, Right, Bottom. (You might want to memorize that order because that’s the order you’ll define the properties in the Thickness constructor as well as in XAML.) The Thickness structure also defines constructors for setting the same amount of padding on all four sides or for setting the same amount on the left and right and on the top and bottom. 

A little research in your favorite search engine will reveal that the iOS status bar has a height of 20. (Twenty what? you might ask. Twenty pixels? Actually, no. For now, just think of them as 20 “units.” For much of your Xamarin.Forms programming, you shouldn’t need to bother with numeric sizes, but Chapter 5, “Dealing with sizes,” will provide some guidance when you need to get down to the pixel level.) 

You can accommodate the status bar like so: 

原文

方法一:在页面中包含一个padding

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

            Padding = , , , );
        }
    }
}

方法二:为iOS设置一个Padding(仅适用SAP)

在Label中显示一段文字

  如果是使用的SAP方法,那么会有一个好处,就是可以使用条件编译指令进行扩展。如果要尝试下面的方法,就需要参照上面的步骤创建一个SAP的项目GreetingsSap。

namespace GreetingsSap
{
    public class GreetingsSapPage : ContentPage
    {
        public GreetingsSapPage()
        {
            this.Content = new Label
            {
                Text = "Greetings,Xamarin.Forms!"
            };
#if __IOS__ 

            Padding = , , , );
#endif
        }
    }
}
The #if directive references the conditional compilation symbol __IOS__ , so the Padding property is set only for the iOS project. The results look like this:
However, these conditional compilation symbols affect only the compilation of the program, so they have no effect in a PCL. Is there a way for a PCL project to include different Padding for different platforms? 

原文

  #if指令引用条件编译指令__IOS__,所以Padding属性只会在iOS项目中被设置,然而条件编译符号只对该程序有效,PCL项目中不起作用,下面提供一种方式对PCL项目去针对不同的平台设置一个Padding。

方法三:仅仅为iOS添加一个padding(PCL或者SAP)