Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6514299
  • 博文数量: 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平台

2019-11-01 10:01:15

网格条形图
由网格的子项集合定义的AddVertical和AddHorizontal方法可以一次性将整个视图集合添加到Grid。默认情况下,新行或列的高度或宽度为“ *”(星号),从而生成的网格包含多个行或列,每个行或列的大小相同。
让我们使用AddHorizontal方法制作一个小条形图,其中包含50个具有随机高度的BoxView中元素。GridBarChart程序的XAML文件定义了一个AbsoluteLayout此框架使用的覆盖图,以显示条形图中特定条形图的信息。它的不透明度设置为0,因此它最初是不可见的:

代码隐藏文件创建了50个BoxView元素,其随机高度请求属性介于0和300之间。因此,每个BoxView的StyleId属性都被替换为一个字符串,该字符串由交替的随机辅音和元音组成,所有这些BoxView元素都累积在一个通用的列表集合中,然后添加到Grid中。该作业是构造函数中的大部分代码:

点击(此处)折叠或打开

  1. public partial class GridBarChartPage : ContentPage
  2. {
  3.     const int COUNT = 50;
  4.     Random random = new Random();
  5.     public GridBarChartPage()
  6.     {
  7.         InitializeComponent();
  8.         List<View> views = new List<View>();
  9.         TapGestureRecognizer tapGesture = new TapGestureRecognizer();
  10.         tapGesture.Tapped += OnBoxViewTapped;
  11.         // Create BoxView elements and add to List.
  12.         for (int i = 0; i < COUNT; i++)
  13.         {
  14.             BoxView boxView = new BoxView
  15.             {
  16.                 Color = Color.Accent,
  17.                 HeightRequest = 300 * random.NextDouble(),
  18.                 VerticalOptions = LayoutOptions.End,
  19.                 StyleId = RandomNameGenerator()
  20.             };
  21.             boxView.GestureRecognizers.Add(tapGesture);
  22.             views.Add(boxView);
  23.         }
  24.         // Add whole List of BoxView elements to Grid.
  25.         grid.Children.AddHorizontal(views);
  26.         // Start a timer at the frame rate.
  27.         Device.StartTimer(TimeSpan.FromMilliseconds(15), OnTimerTick);
  28.     }
  29.     // Arrays for Random Name Generator.
  30.     string[] vowels = { "a", "e", "i", "o", "u", "ai", "ei", "ie", "ou", "oo" };
  31.     string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
  32.                             "n", "p", "q", "r", "s", "t", "v", "w", "x", "z" };
  33.     string RandomNameGenerator()
  34.     {
  35.         int numPieces = 1 + 2 * random.Next(1, 4);
  36.         StringBuilder name = new StringBuilder();
  37.         for (int i = 0; i < numPieces; i++)
  38.         {
  39.             name.Append(i % 2 == 0 ?
  40.             consonants[random.Next(consonants.Length)] :
  41.             vowels[random.Next(vowels.Length)]);
  42.         }
  43.         name[0] = Char.ToUpper(name[0]);
  44.         return name.ToString();
  45.     }
  46.     // Set text to overlay Label and make it visible.
  47.     void OnBoxViewTapped(object sender, EventArgs args)
  48.     {
  49.         BoxView boxView = (BoxView)sender;
  50.         label.Text = String.Format("The individual known as {0} " +
  51.                                    "has a height of {1} centimeters.",
  52.                                    boxView.StyleId, (int)boxView.HeightRequest);
  53.         overlay.Opacity = 1;
  54.     }
  55.     // Decrease visibility of overlay.
  56.     bool OnTimerTick()
  57.     {
  58.         overlay.Opacity = Math.Max(0, overlay.Opacity - 0.0025);
  59.         return true;
  60.     }
  61. }

ChildrenCollection的AddHorizontal方法将多个BoxView元素添加到网格中,并为它们提供顺序的Grid.Column设置。默认情况下,每列的宽度为“ *”(星号),因此每个BoxView的宽度在XAML文件中设置为网格的间距值1提供了条形图条形之间的一点间隔:
201810012051330408
当您将手机侧向转动以增加宽度时,条形更明显:
201810012052200409
此程序还有另一个功能:当您点击其中一个条形图时,覆盖图将变为可见并显示有关该条形图条的信息-特别是来自StyleId的行星际访客名称和条形图的。高度但是构造函数中设置的计时器会不断降低叠加层上的不透明度值,因此此信息会逐渐淡出视图:
201810012054210410
即使没有原生图形系统,Xamarin.Forms也能够显示看起来非常像图形的东西

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