|
34 | 34 | <summary>A <see cref="T:Microsoft.Maui.Controls.View" /> control for picking an element in a list.</summary> |
35 | 35 | <remarks> |
36 | 36 | <para>The visual representation of a Picker is similar to a <see cref="T:Microsoft.Maui.Controls.Entry" />, but a picker control appears in place of a keyboard.</para> |
37 | | - <para>The following example shows the creation of a Picker.</para> |
38 | | - <example> |
39 | | - <code lang="csharp lang-csharp"><![CDATA[using System; |
40 | | -using System.Collections.Generic; |
41 | | -using Microsoft.Maui.Controls; |
42 | | - |
43 | | -namespace FormsGallery |
44 | | -{ |
45 | | - class PickerDemoPage : ContentPage |
46 | | - { |
47 | | - // Dictionary to get Color from color name. |
48 | | - Dictionary<string, Color> nameToColor = new Dictionary<string, Color> |
49 | | - { |
50 | | - { "Aqua", Color.Aqua }, { "Black", Color.Black }, |
51 | | - { "Blue", Color.Blue }, { "Fuchsia", Color.Fuchsia }, |
52 | | - { "Gray", Color.Gray }, { "Green", Color.Green }, |
53 | | - { "Lime", Color.Lime }, { "Maroon", Color.Maroon }, |
54 | | - { "Navy", Color.Navy }, { "Olive", Color.Olive }, |
55 | | - { "Purple", Color.Purple }, { "Red", Color.Red }, |
56 | | - { "Silver", Color.Silver }, { "Teal", Color.Teal }, |
57 | | - { "White", Color.White }, { "Yellow", Color.Yellow } |
58 | | - }; |
59 | | - |
60 | | - public PickerDemoPage() |
61 | | - { |
62 | | - Label header = new Label |
63 | | - { |
64 | | - Text = "Picker", |
65 | | - FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)), |
66 | | - HorizontalOptions = LayoutOptions.Center |
67 | | - }; |
68 | | - |
69 | | - Picker picker = new Picker |
70 | | - { |
71 | | - Title = "Color", |
72 | | - VerticalOptions = LayoutOptions.CenterAndExpand |
73 | | - }; |
74 | | - |
75 | | - foreach (string colorName in nameToColor.Keys) |
76 | | - { |
77 | | - picker.Items.Add(colorName); |
78 | | - } |
79 | | - |
80 | | - // Create BoxView for displaying picked Color |
81 | | - BoxView boxView = new BoxView |
82 | | - { |
83 | | - WidthRequest = 150, |
84 | | - HeightRequest = 150, |
85 | | - HorizontalOptions = LayoutOptions.Center, |
86 | | - VerticalOptions = LayoutOptions.CenterAndExpand |
87 | | - }; |
88 | | - |
89 | | - picker.SelectedIndexChanged += (sender, args) => |
90 | | - { |
91 | | - if (picker.SelectedIndex == -1) |
92 | | - { |
93 | | - boxView.Color = Color.Default; |
94 | | - } |
95 | | - else |
96 | | - { |
97 | | - string colorName = picker.Items[picker.SelectedIndex]; |
98 | | - boxView.Color = nameToColor[colorName]; |
99 | | - } |
100 | | - }; |
101 | | - |
102 | | - // Accomodate iPhone status bar. |
103 | | - this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); |
104 | | - |
105 | | - // Build the page. |
106 | | - this.Content = new StackLayout |
107 | | - { |
108 | | - Children = |
109 | | - { |
110 | | - header, |
111 | | - picker, |
112 | | - boxView |
113 | | - } |
114 | | - }; |
115 | | - |
116 | | - } |
117 | | - } |
118 | | -} |
119 | | -]]></code> |
120 | | - </example> |
121 | 37 | <para> |
122 | 38 | <img href="~/xml/Microsoft.Maui.Controls/_images/Picker.TripleScreenShot.png" /> |
123 | 39 | </para> |
|
0 commit comments