Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6540218
  • 博文数量: 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-02-28 21:36:13

滚动内容
请记住,Xamarin.Forms程序可以访问.NET基类库,并可以使用.NET反射来获取有关程序集中定义的所有类和结构的信息,例如Xamarin.Forms.Core。 这表明可以自动获取Color结构的静态字段和属性。
大多数.NET反射都以Type对象开始。 您可以使用C#typeof运算符为任何类或结构获取Type对象。 例如,表达式typeof(Color)为Color结构返回一个Type对象。
在PCL中可用的.NET版本中,名为GetTypeInfo的Type类的扩展方法返回一个TypeInfo对象,从中可以获取附加信息。 虽然这在下面的程序中不是必需的; 它需要为Type类定义的其他扩展方法,名为GetRuntimeFields和GetRuntimeProperties。 它们以FieldInfo和PropertyInfo对象的集合的形式返回类型的字段和属性。 从这些,名称以及属性的值可以获得。
这由ReflectedColors程序演示。 ReflectedColorsPage.cs文件需要System.Reflection的using指令。
在两个单独的foreach语句中,ReflectedColorsPage类遍历Color结构的所有字段和属性。 对于所有返回Color值的公共静态成员,这两个循环调用CreateColorLabel以创建一个带有Color值和名称的Label,然后将该Label添加到StackLayout。
通过包含所有公共静态字段和属性,程序会列出Color.Transparent,Color.Default和Color.Accent以及前面程序中显示的17个静态字段。 一个单独的CreateColorLabel方法为每个项目创建一个Label视图。 以下是ReflectedColorsPage类的完整列表。

点击(此处)折叠或打开

  1. public class ReflectedColorsPage : ContentPage
  2. {
  3.     public ReflectedColorsPage()
  4.     {
  5.         StackLayout stackLayout = new StackLayout();
  6.         // Loop through the Color structure fields.
  7.         foreach (FieldInfo info in typeof(Color).GetRuntimeFields())
  8.         {
  9.             // Skip the obsolete (i.e. misspelled) colors.
  10.             if (info.GetCustomAttribute<ObsoleteAttribute>() != null)
  11.                 continue;
  12.             if (info.IsPublic &&
  13.                 info.IsStatic &&
  14.                 info.FieldType == typeof(Color))
  15.             {
  16.                 stackLayout.Children.Add(
  17.                 CreateColorLabel((Color)info.GetValue(null), info.Name));
  18.             }
  19.         }
  20.         // Loop through the Color structure properties.
  21.         foreach (PropertyInfo info in typeof(Color).GetRuntimeProperties())
  22.         {
  23.             MethodInfo methodInfo = info.GetMethod;
  24.             if (methodInfo.IsPublic &&
  25.                 methodInfo.IsStatic &&
  26.                 methodInfo.ReturnType == typeof(Color))
  27.             {
  28.                 stackLayout.Children.Add(
  29.                 CreateColorLabel((Color)info.GetValue(null), info.Name));
  30.             }
  31.         }
  32.         Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5);
  33.         // Put the StackLayout in a ScrollView.
  34.         Content = new ScrollView
  35.         {
  36.             Content = stackLayout
  37.         };
  38.     }
  39.     Label CreateColorLabel(Color color, string name)
  40.     {
  41.         Color backgroundColor = Color.Default;
  42.         if (color != Color.Default)
  43.         {
  44.             // Standard luminance calculation.
  45.             double luminance = 0.30 * color.R +
  46.             0.59 * color.G +
  47.             0.11 * color.B;
  48.             backgroundColor = luminance > 0.5 ? Color.Black : Color.White;
  49.         }
  50.         // Create the Label.
  51.         return new Label
  52.         {
  53.             Text = name,
  54.             TextColor = color,
  55.             FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
  56.             BackgroundColor = backgroundColor
  57.         };
  58.     }
  59. }
在构造函数的最后,StackLayout被设置为ScrollView的Content属性,然后将其设置为页面的Content属性。
该类中的CreateColorLabel方法试图通过设置一个配置背景来使每个颜色可见。 该方法基于红色,绿色和蓝色分量的标准加权平均值计算亮度值,然后选择白色或黑色的背景。
此技术对透明无效,因此该项目根本无法显示,并且该方法将Color.Default视为特殊情况,并在Color.Default背景下显示该颜色(无论它可能是什么)。
以下是结果,这些结果仍然不够美观:

但是您可以滚动显示,因为StackLayout是ScrollView的子项。

StackLayout和ScrollView在类层次结构中相关。 StackLayout派生自Layout ,你会记得Layout 类定义了StackLayout继承的Children属性。 通用布局类派生自非布局布局类,而ScrollView也派生自此非布局布局。 理论上,ScrollView是一种布局对象 - 即使它只有一个子对象。
从截图中可以看到,Label的背景颜色扩展到StackLayout的全部宽度,这意味着每个Label都与StackLayout一样宽。
让我们试验一下,以更好地理解Xamarin.Forms布局。 对于这些实验,您可能需要临时给出StackLayout和ScrollView独特的背景颜色:

点击(此处)折叠或打开

  1. public ReflectedColorsPage()
  2. {
  3.     StackLayout stackLayout = new StackLayout
  4.     {
  5.         BackgroundColor = Color.Blue
  6.     };
  7.     …
  8.     Content = new ScrollView
  9.     {
  10.         BackgroundColor = Color.Red,
  11.         Content = stackLayout
  12.     };
  13. }

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