在Label中显示一段文字
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中显示一段文字的更多相关文章
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
- 在Excel表格中输入一大段文字
1.有时为了注释的需要,在excel中需要输入一大段文字,这时候可以使用—视图-工具-绘图,然后选择下面的文本框,即可自定义文本框大小,如需要文本框和表格边框完全重合,在鼠标画文本框时按住 Alt键.
- 小技巧,如何在Label中显示图片
这个需求其实是有的,比如QQ聊天界面里面发送的信息,可以用label来显示文字(也可以用button显示),但是有时候用户可能会发送图片.如果能让Label遇到文字就显示文字,遇到图片就显示图片就好了 ...
- NieR:Automata中的一段文字
还没开始玩这个游戏,但在网易云音乐上听到一首歌,很好听 http://music.163.com/#/m/song?id=468490570 搜了一下相关视频,发现这首歌是在与一个叫做歌姬的boss战 ...
- Python3 tkinter基础 LabelFrame StringVar 单击按钮,Label中显示的文字更换
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- laravel中使一段文字,限制长度,并且超出部分使用指定内容代替
{{str_limit($post->content,100,'....')}} 文字内容超出100个字,就用省略号显示
- iOS 如何在Label中显示html的文本
if (self.messageModel) { NSString * htmlString = self.messageModel.contentText; NSAttributedString * ...
- iOS在一个label中显示不同颜色的字体
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 300, 30)]; NSMutableAttributedSt ...
- react 简单在页面中输出一段文字
之前用脚手架创建了一个react项目,将react自带的src文件夹删除后创建一个空的src文件夹 在src文件夹中创建一个index.jsx文件作为JS入口文件并创建一个hello组件 现在我们进入 ...
随机推荐
- Android AutoLayout全新的适配方式 堪称适配终结者(转)
一.概述 相信Android的开发者对于设配问题都比较苦恼,Google官方虽然给出了一系列的建议,但是想要单纯使用这些建议将设备很轻松的做好,还是相当困难的.个人也比较关注适配的问题,之前也发了几篇 ...
- bzoj 3109: [cqoi2013]新数独
#include<cstdio> #include<iostream> using namespace std; ][],li[][],xi[][],a[][],bh[][], ...
- 关于ADDED_TO_STAGE事件
可视类初始化的时候,很多时候要用到stage属性,则要使用Event.ADDED_TO_STAGE事件,这个swf被其它的文件加载,如果直接在初始化函数内使用stage属性 .但是,文档类初始化函数内 ...
- Windows Server 2003 Sp2 下无法安装SQL Server 2008 Management Studio Express问题
Windows Server 2003 Sp2 下无法安装SQL Server 2008 Management Studio Express问题钉子 发表于 2010-5-22 1:42:51问题描述 ...
- iOS移动下上传图片失败解决 (上传多图,带其他参数)
项目中有一个主要的功能,就是上传图片,结结果移动真的是很奇怪,WiFi,联通,电信都没有问题的情况下,居然在移动下不行,真的是很头疼.不过好在最后是解决了 项目的网络请求我是采用ASIHttpRequ ...
- 记录一些容易忘记的属性 -- UITabBarController
UIViewController中的 @property(nonatomic,copy) NSString *title; // Localized title for use by a pare ...
- 2014年3月份第3周51Aspx源码发布详情
WPF翻书效果源码 2014-3-17 [VS2010]源码描述:WPF翻书效果源码:多点触控的一个Demo,利用鼠标可以实现图书翻页效果:适合新手学习研究. TL简单家具网新手源码 2014-3 ...
- 福州月赛2057 DFS
题意:告诉你族谱,然后Q条查询s和t的关系,妈妈输出M,爸爸输出F: 题目地址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=78233# ...
- Xml Schema:C#访问在complextype中插入新元素
最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSe ...
- Android关联源码support-v4,v7,v13源码(转)
在Android实际开发过程中往往会遇到使用v4,v7或v13兼容包中的一些类如ViewPager,Fragment等,但却无法关联源码. 在网上搜索之后,有很多办法,这里只向大家介绍一种,我用的觉得 ...