Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6538324
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-07-06 11:56:53

内容属性特性

ScaryColorList程序中的XAML文件实际上比它需要的时间稍长。 您可以删除ContentPage.Content标签,所有StackLayout.Children标签和所有Frame.Content标签,并且程序的工作原理是相同的:



点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="ScaryColorList.ScaryColorListPage">
  4.  
  5.     <ContentPage.Padding>
  6.         <OnPlatform x:TypeArguments="Thickness"
  7.                     iOS="0, 20, 0, 0" />
  8.     </ContentPage.Padding>
  9.     <StackLayout>
  10.         <Frame OutlineColor="Accent">
  11.             <StackLayout Orientation="Horizontal">
  12.                 <BoxView Color="Red" />
  13.                 <Label Text="Red"
  14.                        VerticalOptions="Center" />
  15.             </StackLayout>
  16.         </Frame>
  17.         <Frame OutlineColor="Accent">
  18.             <StackLayout Orientation="Horizontal">
  19.                 <BoxView Color="Green" />
  20.                 <Label Text="Green"
  21.                        VerticalOptions="Center" />
  22.             </StackLayout>
  23.         </Frame>
  24.         <Frame OutlineColor="Accent">
  25.             <StackLayout Orientation="Horizontal">
  26.                 <BoxView Color="Blue" />
  27.                 <Label Text="Blue"
  28.                        VerticalOptions="Center" />
  29.             </StackLayout>
  30.         </Frame>
  31.     </StackLayout>
  32. </ContentPage>


它现在看起来很干净。 剩下的唯一属性元素是ContentPage的Padding属性。
与几乎所有关于XAML语法的内容一样,某些属性元素的消除是由底层类支持的。 XAML中使用的每个类都被允许将一个属性定义为一个隐含属性(有时也称为该类的默认属性)。 对于此内容属性,不需要属性元素标记,并且开始和结束标记内的任何XML内容都会自动分配给此属性。 非常方便,ContentPage的内容属性是Content,StackLayout的内容属性是Children,Frame的content属性是Content。
这些内容属性被记录下来,但你需要知道在哪里看。 一个类通过使用ContentPropertyAttribute来指定其内容属性。 如果该属性附加到一个类中,它将与类声明一起出现在联机Xamarin.Forms API文档中。 以下是它在ContentPage文档中的显示方式:

点击(此处)折叠或打开

  1. [Xamarin.Forms.ContentProperty("Content")]
  2. public class ContentPage : TemplatedPage

如果你大声说出来,这听起来有点多余:Content属性是ContentPage的内容属性。
Frame类的声明类似于:

 

点击(此处)折叠或打开

  1. [Xamarin.Forms.ContentProperty("Content")]
  2. public class Frame : ContentView

StackLayout没有应用ContentProperty特性,但StackLayout派生自Layout ,Layout 具有ContentProperty特性:

 

点击(此处)折叠或打开

  1. [Xamarin.Forms.ContentProperty("Children")]
  2. public abstract class Layout<T> : Layout, IViewContainer<T> where T : View

ContentProperty特性由派生自Layout 的类继承,所以Children是StackLayout的内容属性。
当然,如果您在不需要时包含属性元素,则没有问题,但在大多数情况下,它们将不再出现在本书的示例程序中。

格式化文本

由XAML文件显示的文本可能只涉及一两个单词,但有时需要整个段落,可能是一些嵌入式字符格式。 在XAML中指定字符格式并不总是那么明显,或者很容易,因为我们熟悉HTML。
TextVariations解决方案具有一个XAML文件,该文件在可滚动的StackLayout中包含七个Label视图:

 

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="TextVariations.TextVariationsPage">
  4.     <ContentPage.Padding>
  5.         <OnPlatform x:TypeArguments="Thickness"
  6.                     iOS="0, 20, 0, 0" />
  7.     </ContentPage.Padding>
  8.     <ScrollView>
  9.         <StackLayout>
  10.         </StackLayout>
  11.     </ScrollView>
  12. </ContentPage>

七个标签视图中的每一个都显示了定义显示文本的不同方式。 出于参考目的,这里是在所有三个平台上运行的程序:
201806282315540343
最简单的方法只需要在Label元素的Text属性中设置几个字:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand"
  2.        Text="Single lines of text are easy." />

您也可以通过将Text属性分解为属性元素来设置Text属性:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand">
  2.     <Label.Text>
  3.         Text can also be content of the Text property.
  4.     </Label.Text>
  5.  </Label>

Text是Label的内容属性,所以你不需要Label.Text标签:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand">
  2.     Text is the content property of Label.
  3. </Label>

当您将文本设置为标签的内容(不管您是否使用Label.Text标签)时,文本将被裁剪:从文本的开头和结尾删除包括回车符在内的所有空白字符。 但是,所有嵌入的空白都会保留,包括行尾字符。
将Text属性设置为属性属性时,引号内的所有空白都会保留,但如果文本占用XAML文件中的多行,则每行尾字符(或字符序列)都将转换为 一个空间。
因此,显示一个统一格式的文本的整个段落有些问题。 最简单的方法是将文本设置为属性属性。 您可以将整个段落放在XAML文件中作为一行,但如果您更喜欢使用多行,则应该在由引号包围的XAML文件中将整个图形对齐,如下所示:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand"
  2.  Text=
  3. "Perhaps the best way to define a paragraph of
  4. uniformly formatted text is by setting the Text
  5. property as an attribute and left justifying
  6. the block of text in the XAML file. End-of-line
  7. characters are converted to a space character." />

行尾字符被转换为空格字符,因此各行都被适当地关联。 但要小心:不要将任何流浪的角色留在单行的末尾或开头。 这些将在该段落中显示为无关字符。
当多行文本被指定为标签的内容时,仅在文本的开始和结尾处的空白被裁剪。 所有嵌入的空白都会保留,包括行尾字符:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand">
  2. Text as content has the curse
  3. Of breaks at each line's close.
  4. That's a format great for verse
  5. But not the best for prose.
  6.  </Label>

该文本呈现为四行。 如果您在Xamarin.Forms应用程序中显示列表或诗歌,那正是您想要的。 否则,可能不会。
如果您的行或段落文本需要某些不一致的段落格式,则您需要使用Label的FormattedText属性。 您可能还记得,您将其设置为FormattedString对象,然后将多个Span对象设置为FormattedString的Spans集合。 在XAML中,您需要Label.FormattedString的property-element标签,但是Spans是FormattedString的内容属性:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand">
  2.     <Label.FormattedText>
  3.         <FormattedString>
  4.             <Span Text="A single line with " />
  5.             <Span Text="bold" FontAttributes="Bold" />
  6.             <Span Text=" and " />
  7.             <Span Text="italic" FontAttributes="Italic" />
  8.             <Span Text=" and " />
  9.             <Span Text="large" FontSize="Large" />
  10.             <Span Text=" text." />
  11.         </FormattedString>
  12.     </Label.FormattedText>
  13.  </Label>

请注意,非格式化项目的文本属性在文本字符串的末尾或开头处有空格,或两者都有,因此这些项目不会彼此碰撞。
但是,在一般情况下,您可能正在处理整个段落。 您可以将Span的Text属性设置为长行,或者可以将其包装在多行上。 与标签一样,将整个块保留在XAML文件中左对齐:

 

点击(此处)折叠或打开

  1. <Label VerticalOptions="CenterAndExpand">
  2.     <Label.FormattedText>
  3.         <FormattedString>
  4.             <Span Text=
  5. "A paragraph of formatted text requires left justifying
  6. it within the XAML file. But the text can include multiple
  7. kinds of character formatting, including " />
  8.             <Span Text="bold" FontAttributes="Bold" />
  9.             <Span Text=" and " />
  10.             <Span Text="italic" FontAttributes="Italic" />
  11.             <Span Text=" and " />
  12.             <Span Text="large" FontSize="Large" />
  13.             <Span Text=
  14. " and whatever combinations you might desire to adorn
  15. your glorious prose." />
  16.         </FormattedString>
  17.     </Label.FormattedText>
  18.  </Label>

您会在截图中注意到,具有较大字体大小的文本与基线上的常规文本对齐,这是印刷适当的方法,并调整了行间距以适应较大的文本。
在大多数Xamarin.Forms程序中,XAML和代码都不是孤立存在,而是一起工作。 XAML中的元素可以触发代码中处理的事件,代码可以修改XAML中的元素。 在下一章中,你会看到这是如何工作的。



阅读(1987) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~