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,编译和运行后可以看到下面的效果:

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)

  如果是使用的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)

在Label中显示一段文字的更多相关文章

  1. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  2. 在Excel表格中输入一大段文字

    1.有时为了注释的需要,在excel中需要输入一大段文字,这时候可以使用—视图-工具-绘图,然后选择下面的文本框,即可自定义文本框大小,如需要文本框和表格边框完全重合,在鼠标画文本框时按住 Alt键.

  3. 小技巧,如何在Label中显示图片

    这个需求其实是有的,比如QQ聊天界面里面发送的信息,可以用label来显示文字(也可以用button显示),但是有时候用户可能会发送图片.如果能让Label遇到文字就显示文字,遇到图片就显示图片就好了 ...

  4. NieR:Automata中的一段文字

    还没开始玩这个游戏,但在网易云音乐上听到一首歌,很好听 http://music.163.com/#/m/song?id=468490570 搜了一下相关视频,发现这首歌是在与一个叫做歌姬的boss战 ...

  5. Python3 tkinter基础 LabelFrame StringVar 单击按钮,Label中显示的文字更换

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. laravel中使一段文字,限制长度,并且超出部分使用指定内容代替

    {{str_limit($post->content,100,'....')}} 文字内容超出100个字,就用省略号显示

  7. iOS 如何在Label中显示html的文本

    if (self.messageModel) { NSString * htmlString = self.messageModel.contentText; NSAttributedString * ...

  8. iOS在一个label中显示不同颜色的字体

    UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 300, 30)]; NSMutableAttributedSt ...

  9. react 简单在页面中输出一段文字

    之前用脚手架创建了一个react项目,将react自带的src文件夹删除后创建一个空的src文件夹 在src文件夹中创建一个index.jsx文件作为JS入口文件并创建一个hello组件 现在我们进入 ...

随机推荐

  1. Android开发--ScrollView的应用

    1.简介 当内容无法全部显示时,需要采取滚动的方式获取其与内容.其中,ScrollView为垂直滚动控件,HorizontalScrollView为水平滚动控件. 2.构建

  2. 分布式系统中一些主要的副本更新策略——Dynamo/Cassandra/Riak同时采取了主从式更新的同步+异步类型,以及任意节点更新的策略。

    分布式系统中一些主要的副本更新策略. 1.同时更新 类型A:没有任何协议,可能出现多个节点执行顺序交叉导致数据不一致情况. 类型B:通过一致性协议唯一确定不同更新操作的执行顺序,从而保证数据一致性 2 ...

  3. 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】

    Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...

  4. mysql 处理查询请求过程

    需要搞清楚查询为什么会慢,就要搞清楚mysql处理查询请求的过程: 1.客户端发送SQL请求给服务器 2.服务器检查是否可以在查询缓存中命中该SQL   查询缓存对SQL性能的影响. 1.需要对缓存加 ...

  5. web项目的日志打印位置设置

    1, 若在项目中放logback.groovy文件(如: src/test/resource下),则日志会打印到控制台上. logback.groovy 内容如下: // // Built on Fr ...

  6. 我的套路(windows):Jenkins+Jmeter+Ant持续集成

    前期准备: 1.Jdk1.6或以上:http://www.oracle.com/technetwork/java/javase/downloads/index.html 命令行输入:java -ver ...

  7. 我是一只IT小小鸟

    不知不觉中走过了高三的时光,最后也没抓住时间的尾巴,不得不承认自己已经到了大一下学期了.接触了大学生职业生涯规划这门课程,一开始认为学习了这门课程以后就会对自己的未来有一个规划,渐渐的去意识到软件工程 ...

  8. jQuery.extend源码深层分析

    在网站的开发中,经常会自己写一些jQuery插件来方便使用,其中自然少不了一个关键的方法->jQuery.extend(),使用这个方法来扩展jQuery对象. 那么今天就来讲讲这个函数的实现原 ...

  9. ffmepg-nginx-nginx-rtmp-module配置脚本

    把上个月写的的配置脚本贴一下: #!/bin/bash #version:-- #create by itn #dis: this is used to auto install ffmpeg+ngi ...

  10. 实时刷新Winform中Label的Text

    最直白的例子: private void btnStart_Click(object sender, EventArgs e) { ; ) { labelTime.Text = i.ToString( ...