Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6535963
  • 博文数量: 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-24 21:55:53

访问静态成员

IMarkupExtension最简单和最有用的实现之一封装在StaticExtension类中。 这是原始XAML规范的一部分,因此它通常出现在带有x前缀的XAML中。 StaticExtension定义了一个名为Member of string的属性,您可以将其设置为公共常量,静态属性,静态字段或枚举成员的类和成员名称。
让我们看看它是如何工作的。 这是一个标签,其中六个属性设置为通常在XAML中显示的属性:

点击(此处)折叠或打开

  1. <Label Text="Just some text"
  2.        BackgroundColor="Accent"
  3.        TextColor="Black"
  4.        FontAttributes="Italic"
  5.        VerticalOptions="Center"
  6.        HorizontalTextAlignment="Center" />


其中五个属性设置为最终引用各种静态属性,字段和枚举成员的文本字符串,但这些文本字符串的转换通过类型转换器和枚举类型的标准XAML解析进行。

如果您希望更明确地将这些属性设置为各种静态属性,字段和枚举成员,则可以在属性元素标记中使用x:StaticExtension:

点击(此处)折叠或打开

  1. <Label Text="Just some text">
  2.     <Label.BackgroundColor>
  3.         <x:StaticExtension Member="Color.Accent" />
  4.     </Label.BackgroundColor>
  5.     <Label.TextColor>
  6.         <x:StaticExtension Member="Color.Black" />
  7.     </Label.TextColor>
  8.     <Label.FontAttributes>
  9.         <x:StaticExtension Member="FontAttributes.Italic" />
  10.     </Label.FontAttributes>
  11.     <Label.VerticalOptions>
  12.         <x:StaticExtension Member="LayoutOptions.Center" />
  13.     </Label.VerticalOptions>
  14.     <Label.HorizontalTextAlignment>
  15.         <x:StaticExtension Member="TextAlignment.Center" />
  16.     </Label.HorizontalTextAlignment>
  17. </Label>


Color.Accent是一个静态属性。 Color.Black和LayoutOptions.Center是静态字段。 FontAttributes.Italic和TextAlignment.Center是枚举成员。

考虑到使用文本字符串设置这些属性的难易程度,使用Stat?icExtension的方法最初看起来很荒谬,但请注意它是一种通用机制。 如果StaticExtension标记的类型与target属性的类型匹配,则可以使用StaticExtension标记中的任何静态属性,字段或枚举成员。
按照惯例,实现IMarkupExtension的类在其名称中包含单词Extension,但您可以将其保留在XAML中,这就是为什么此标记扩展通常称为x:Static而不是x:StaticExtension。 以下标记略微短于前一个块:

点击(此处)折叠或打开

  1. <Label Text="Just some text">
  2.     <Label.BackgroundColor>
  3.         <x:Static Member="Color.Accent" />
  4.     </Label.BackgroundColor>
  5.     <Label.TextColor>
  6.         <x:Static Member="Color.Black" />
  7.     </Label.TextColor>
  8.     <Label.FontAttributes>
  9.         <x:Static Member="FontAttributes.Italic" />
  10.     </Label.FontAttributes>
  11.     <Label.VerticalOptions>
  12.         <x:Static Member="LayoutOptions.Center" />
  13.     </Label.VerticalOptions>
  14.     <Label.HorizontalTextAlignment>
  15.         <x:Static Member="TextAlignment.Center" />
  16.     </Label.HorizontalTextAlignment>
  17. </Label>

现在,对于真正重大的主要 - 语法的改变导致属性元素标签消失,占用空间大大缩小。 XAML标记扩展几乎与标记扩展名和一对花括号中的参数一起出现:

点击(此处)折叠或打开

  1. <Label Text="Just some text"
  2.        BackgroundColor="{x:Static Member=Color.Accent}"
  3.        TextColor="{x:Static Member=Color.Black}"
  4.        FontAttributes="{x:Static Member=FontAttributes.Italic}"
  5.        VerticalOptions="{x:Static Member=LayoutOptions.Center}"
  6.        HorizontalTextAlignment="{x:Static Member=TextAlignment.Center}" />

这种带有花括号的语法无处不在地与XAML标记扩展一起使用,许多开发人员认为标记扩展与大括号语法同义。 这几乎是正确的:虽然花括号总是表示存在XAML标记扩展,但在许多情况下,标记扩展可以在没有花括号的XAML中出现(如前所示),并且有时可以方便地使用它们。
staticExtension类的operty不再是XML属性。 就XML而言,由花括号分隔的整个表达式是属性的值,并且花括号内的参数不带引号。
就像元素一样,标记扩展可以具有ContentProperty属性。 仅具有一个属性的标记扩展(例如具有单个成员属性的StaticExtension类)始终将该唯一属性标记为内容属性。 对于使用卷括号语法的标记扩展,这意味着可以删除成员属性名称和等号:

点击(此处)折叠或打开

  1. <Label Text="Just some text"
  2.        BackgroundColor="{x:Static Color.Accent}"
  3.        TextColor="{x:Static Color.Black}"
  4.        FontAttributes="{x:Static FontAttributes.Italic}"
  5.        VerticalOptions="{x:Static LayoutOptions.Center}"
  6.        HorizontalTextAlignment="{x:Static TextAlignment.Center}" />

这是x:Static标记扩展的常见形式。
显然,不需要对这些特定属性使用x:Static,但是您可以定义自己的静态成员来实现应用程序范围的常量,并且可以在XAML文件中引用它们。 这在SharedStatics项目中得到了证明。
SharedStatics项目包含一个名为AppConstants的类,它定义了一些可能用于格式化文本的常量和静态字段:

点击(此处)折叠或打开

  1. namespace SharedStatics
  2. {
  3.     static class AppConstants
  4.     {
  5.         public static Color LightBackground = Color.Yellow;
  6.         public static Color DarkForeground = Color.Blue;
  7.         public static double NormalFontSize = 18;
  8.         public static double TitleFontSize = 1.4 * NormalFontSize;
  9.         public static double ParagraphSpacing = 10;
  10.         public const FontAttributes Emphasis = FontAttributes.Italic;
  11.         public const FontAttributes TitleAttribute = FontAttributes.Bold;
  12.         public const TextAlignment TitleAlignment = TextAlignment.Center;
  13.     }
  14. }

如果每个平台需要不同的东西,可以在这些定义中使用Device.OnPlatform。
然后,XAML文件使用18 x:静态标记扩展来引用这些项。 请注意将本地前缀与项目命名空间相关联的XML名称空间声明:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              xmlns:local="clr-namespace:SharedStatics"
  4.              x:Class="SharedStatics.SharedStaticsPage"
  5.              BackgroundColor="{x:Static local:AppConstants.LightBackground}">
  6.     <ContentPage.Padding>
  7.         <OnPlatform x:TypeArguments="Thickness"
  8.                     iOS="0, 20, 0, 0" />
  9.     </ContentPage.Padding>
  10.     <StackLayout Padding="10, 0"
  11.                  Spacing="{x:Static local:AppConstants.ParagraphSpacing}">
  12.  
  13.         <Label Text="The SharedStatics Program"
  14.                TextColor="{x:Static local:AppConstants.DarkForeground}"
  15.                FontSize="{x:Static local:AppConstants.TitleFontSize}"
  16.                FontAttributes="{x:Static local:AppConstants.TitleAttribute}"
  17.                HorizontalTextAlignment="{x:Static local:AppConstants.TitleAlignment}" />
  18.         <Label TextColor="{x:Static local:AppConstants.DarkForeground}"
  19.                FontSize="{x:Static local:AppConstants.NormalFontSize}">
  20.             <Label.FormattedText>
  21.                 <FormattedString>
  22.                     <Span Text="Through use of the " />
  23.                     <Span Text="x:Static"
  24.                           FontSize="{x:Static local:AppConstants.NormalFontSize}"
  25.                           FontAttributes="{x:Static local:AppConstants.Emphasis}" />
  26.                     <Span Text=
  27. " XAML markup extension, an application can maintain a collection of
  28. common property settings defined as constants, static properties or fields,
  29. or enumeration members in a separate code file. These can then be
  30. referenced within the XAML file." />
  31.                 </FormattedString>
  32.             </Label.FormattedText>
  33.         </Label>
  34.         <Label TextColor="{x:Static local:AppConstants.DarkForeground}"
  35.                FontSize="{x:Static local:AppConstants.NormalFontSize}">
  36.             <Label.FormattedText>
  37.                 <FormattedString>
  38.                     <Span Text=
  39. "However, this is not the only technique to share property settings.
  40. You'll soon discover that you can store objects in a " />
  41.                         <Span Text="ResourceDictionary"
  42.                           FontSize="{x:Static local:AppConstants.NormalFontSize}"
  43.                           FontAttributes="{x:Static local:AppConstants.Emphasis}" />
  44.                     <Span Text=" and access them through the " />
  45.                     <Span Text="StaticResource"
  46.                           FontSize="{x:Static local:AppConstants.NormalFontSize}"
  47.                           FontAttributes="{x:Static local:AppConstants.Emphasis}" />
  48.                     <Span Text=
  49. " markup extension, and even encapsultate multiple property settings in a " />
  50.                     <Span Text="Style"
  51.                           FontSize="{x:Static local:AppConstants.NormalFontSize}"
  52.                           FontAttributes="{x:Static local:AppConstants.Emphasis}" />
  53.                     <Span Text=" object." />
  54.                 </FormattedString>
  55.             </Label.FormattedText>
  56.         </Label>
  57.     </StackLayout>
  58. </ContentPage>

具有FontAttributes设置的每个Span对象都会重复在Label本身上设置的FontSize设置,因为当应用其他与字体相关的设置时,Span对象不会从Label继承与字体相关的设置。
这是:

此技术允许您在多个页面上使用这些公共属性设置,如果您需要更改值,则只需更改AppSettings文件。
也可以使用x:Static和静态属性以及在外部li?braries中的类中定义的字段。 下面的示例名为SystemStatics,它设计得非常合适 - 它将Button的BorderWidth设置为等于Math类中定义的PI静态字段,并使用静态Environment.New?Line属性来处理文本中的换行符。 但它证明了这项技术。
Math和Environment类都在.NET System命名空间中定义,因此需要新的XML命名空间声明来定义名为(例如)sys for System的前缀。 请注意,此命名空间声明将CLR命名空间指定为System,但将程序集指定为mscorlib,它最初代表Microsoft公共对象运行时库,但现在代表多语言标准公共对象运行时库:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              xmlns:sys="clr-namespace:System;assembly=mscorlib"
  4.              x:Class="SystemStatics.SystemStaticsPage">
  5.     <StackLayout>
  6.         <Button Text=" Button with π border width "
  7.                 BorderWidth="{x:Static sys:Math.PI}"
  8.                 HorizontalOptions="Center"
  9.                 VerticalOptions="CenterAndExpand">
  10.             <Button.BackgroundColor>
  11.                 <OnPlatform x:TypeArguments="Color"
  12.                             Android="#404040" />
  13.             </Button.BackgroundColor>
  14.             <Button.BorderColor>
  15.                 <OnPlatform x:TypeArguments="Color"
  16.                             Android="White"
  17.                             WinPhone="Black" />
  18.             </Button.BorderColor>
  19.         </Button>
  20.         <Label VerticalOptions="CenterAndExpand"
  21.                HorizontalTextAlignment="Center"
  22.                FontSize="Medium">
  23.             <Label.FormattedText>
  24.                 <FormattedString>
  25.                     <Span Text="Three lines of text" />
  26.                     <Span Text="{x:Static sys:Environment.NewLine}" />
  27.                     <Span Text="separated by" />
  28.                     <Span Text="{x:Static sys:Environment.NewLine}" />
  29.                     <Span Text="Environment.NewLine"
  30.                           FontSize="Medium"
  31.                           FontAttributes="Italic" />
  32.                     <Span Text=" strings" />
  33.                 </FormattedString>
  34.             </Label.FormattedText>
  35.         </Label>
  36.     </StackLayout>
  37. </ContentPage>

除非设置了背景颜色,否则按钮边框不会显示在Android中,并且在An?droid和Windows Phone上边框都需要非默认颜色,因此一些额外的标记可以解决这些问题。 在iOS平台上,按钮边框往往会挤压按钮文本,因此文本在开头和结尾都定义了空格。
仅从视觉效果来判断,我们必须相信按钮边框宽度约为3.14单位宽,但断线肯定有效:

使用花括号进行标记扩展意味着您无法显示由花括号括起来的文本。 本文中的花括号将被误认为是标记扩展名:

点击(此处)折叠或打开

  1. <Label Text="{Text in curly braces}" />

那不行。 您可以在文本字符串中的其他位置使用花括号,但不能以左大括号开头。
但是,如果确实需要,可以通过使用由左右一对花括号组成的转义序列开始文本,确保文本不会被误认为是XAML标记扩展:

点击(此处)折叠或打开

  1. <Label Text="{}{Text in curly braces}" />

这将显示您想要的文本。


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