1. Lets 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:
  2. 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!)
  3. 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. Dont pick those!)
  4. In either case, give the new file a name of GreetingsPage.cs.
  5. 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 wont be directly accessed from outside the Greetings project.
  6. Lets 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引用,下面是修改结果:

  1. using System;
  2. using Xamarin.Forms;
  3.  
  4. namespace Greetings
  5. {
  6. public class GreetingsPage : ContentPage
  7. {
  8. public GreetingsPage()
  9. {
  10.  
  11. }
  12. }
  13. }

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

  1. using System;
  2. using Xamarin.Forms;
  3.  
  4. namespace Greetings
  5. {
  6. public class GreetingsPage : ContentPage
  7. {
  8. public GreetingsPage()
  9. {
  10. Label label = new Label();
  11. label.Text = "Greetings,Xamarin.Forms!";
  12. this.Content = label;
  13. }
  14. }
  15. }

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Xamarin.Forms;
  7.  
  8. namespace Greetings
  9. {
  10. public class App : Application
  11. {
  12. public App()
  13. {
  14. MainPage = new GreetingsPage();
  15. }
  16.  
  17. protected override void OnStart()
  18. {
  19. // Handle when your app starts
  20. }
  21.  
  22. protected override void OnSleep()
  23. {
  24. // Handle when your app sleeps
  25. }
  26.  
  27. protected override void OnResume()
  28. {
  29. // Handle when your app resumes
  30. }
  31. }
  32. }
  1. Its easy to forget this step, and youll be puzzled that your program seems to completely ignore your page class and still says "Welcome to Xamarin Forms!"
  2. It is in the GreetingsPage class (and others like it) where youll 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 youll need to write. Of course, you can add additional classes to the project if you need them.
  3. 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 wont include the using directives or the namespace definition.
  4. 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的对象创建和属性初始化风格。

  1.   public class GreetingsPage : ContentPage
  2. {
  3. public GreetingsPage()
  4. {
  5. Label label = new Label()
  6. {
  7. Text = "Greetings,Xamarin.Forms!"
  8. };
  9. this.Content = label;
  10. }
  11. }

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

  1.   public class GreetingsPage : ContentPage
  2. {
  3. public GreetingsPage()
  4. {
  5. this.Content = new Label
  6. {
  7. Text = "Greetings,Xamarin.Forms!"
  8. };
  9. }
  10. }
  1. 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, its not always as simple as this example might indicate if you need to call methods on these objects or set event handlers.
  2. 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, heres what youll see:

原文

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

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

  1. 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.
  2. This problem disappears in multipage-navigation applications discussed later in this book, but until that time, here are four ways (or five ways if youre using an SAP) to solve this problem right away.

原文

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

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

  1. Solution 1. Include padding on the page
  2.  
  3. 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 thats the order youll 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.
  4.  
  5. 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 shouldnt 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.)
  6.  
  7. You can accommodate the status bar like so:

原文

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

  1. namespace Greetings
  2. {
  3. public class GreetingsPage : ContentPage
  4. {
  5. public GreetingsPage()
  6. {
  7. this.Content = new Label
  8. {
  9. Text = "Greetings,Xamarin.Forms!"
  10. };
  11.  
  12. Padding = , , , );
  13. }
  14. }
  15. }

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

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

  1. namespace GreetingsSap
  2. {
  3. public class GreetingsSapPage : ContentPage
  4. {
  5. public GreetingsSapPage()
  6. {
  7. this.Content = new Label
  8. {
  9. Text = "Greetings,Xamarin.Forms!"
  10. };
  11. #if __IOS__
  12.  
  13. Padding = , , , );
  14. #endif
  15. }
  16. }
  17. }
  1. 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:
  2. 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. OC语言BLOCK和协议

    OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似 ...

  2. iOS开发拓展篇—音频处理(音乐播放器2)

    iOS开发拓展篇—音频处理(音乐播放器2) 说明:该文主要介绍音乐播放界面的搭建. 一.跳转 1.跳转到音乐播放界面的方法选择 (1)使用模态跳转(又分为手动的和自动的) (2)使用xib并设置跳转 ...

  3. OpenCV源码分析:RGB到其他色彩空间的转换

    1.流程调用图 2.部分代码分析 //模板函数进行颜色空间的转换 template <typename Cvt> void CvtColorLoop(const Mat& src, ...

  4. [转]HttpURLConnection的使用

    /* * URL请求的类别分为二类,GET与POST请求.二者的区别在于: * a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, * b:) post与get ...

  5. Java中final的作用

    Java中Final可以被用于变量,方法,类.具体来说: 1, Final 变量 修饰主类型时,制定变量为常数,不希望被改变 修饰类类型时,表示变量的句柄不变,不能被指定指向新的变量 修饰参数时,参数 ...

  6. java面试准备之基础排序——冒泡与选择排序

    选择排序:     [java]    public void select(int[] arr){            for(int i=0;i<arr.length;i++){      ...

  7. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  8. JVM-程序编译与代码晚期(运行期)优化

    晚期(运行期)优化 1.为了提高热点代码的执行效率,在运行时,虚拟机将会把这些代码编译成与本地平台相关的机器码,并进行各种层次的优化,完成这个任务的编译器称为即时编译器(Just In Time,JI ...

  9. 隐藏ArcGIS server设置的用户名

    打开注册表编辑器,定位到“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ SpecialAccoun ...

  10. C++异常处理的问题

    一般在C语言中,是通过返回值或者设置errno的方式来标识错误的 但在C++里面,构造函数是没有返回值的,于是发明了异常的方式:为了正确的向使用者表明 异常抛出的原因,你必须弄清楚异常抛出的原因(比如 ...