|
1 | 1 | using System;
|
2 |
| - |
| 2 | +using System.IO; |
3 | 3 | using Xamarin.Forms;
|
4 | 4 |
|
5 | 5 | namespace DependencyServiceSample
|
6 | 6 | {
|
7 |
| - public class App : Application |
8 |
| - { |
9 |
| - public App () |
10 |
| - { |
11 |
| - var stack = new StackLayout (); |
12 |
| - MainPage = new ContentPage { |
13 |
| - Content = stack |
14 |
| - }; |
| 7 | + public class App : Application |
| 8 | + { |
| 9 | + public App() |
| 10 | + { |
| 11 | + var stack = new StackLayout(); |
| 12 | + MainPage = new ContentPage |
| 13 | + { |
| 14 | + Content = stack |
| 15 | + }; |
| 16 | + |
| 17 | + // Text-to-Speech |
| 18 | + var speak = new Button |
| 19 | + { |
| 20 | + Text = "Hello, Forms !", |
| 21 | + VerticalOptions = LayoutOptions.CenterAndExpand, |
| 22 | + HorizontalOptions = LayoutOptions.CenterAndExpand, |
| 23 | + }; |
| 24 | + speak.Clicked += (sender, e) => |
| 25 | + { |
| 26 | + DependencyService.Get<ITextToSpeech>().Speak("Hello from Xamarin Forms"); |
| 27 | + }; |
| 28 | + stack.Children.Add(speak); |
| 29 | + |
| 30 | + // Battery Information |
| 31 | + var button = new Button |
| 32 | + { |
| 33 | + Text = "Click for battery info", |
| 34 | + VerticalOptions = LayoutOptions.CenterAndExpand, |
| 35 | + HorizontalOptions = LayoutOptions.CenterAndExpand, |
| 36 | + }; |
| 37 | + button.Clicked += (sender, e) => |
| 38 | + { |
| 39 | + var bat = DependencyService.Get<IBattery>(); |
15 | 40 |
|
| 41 | + switch (bat.PowerSource) |
| 42 | + { |
| 43 | + case PowerSource.Battery: |
| 44 | + button.Text = "Battery - "; |
| 45 | + break; |
| 46 | + case PowerSource.Ac: |
| 47 | + button.Text = "AC - "; |
| 48 | + break; |
| 49 | + case PowerSource.Usb: |
| 50 | + button.Text = "USB - "; |
| 51 | + break; |
| 52 | + case PowerSource.Wireless: |
| 53 | + button.Text = "Wireless - "; |
| 54 | + break; |
| 55 | + case PowerSource.Other: |
| 56 | + default: |
| 57 | + button.Text = "Unknown - "; |
| 58 | + break; |
| 59 | + } |
| 60 | + switch (bat.Status) |
| 61 | + { |
| 62 | + case BatteryStatus.Charging: |
| 63 | + button.Text += "Charging"; |
| 64 | + break; |
| 65 | + case BatteryStatus.Discharging: |
| 66 | + button.Text += "Discharging"; |
| 67 | + break; |
| 68 | + case BatteryStatus.NotCharging: |
| 69 | + button.Text += "Not Charging"; |
| 70 | + break; |
| 71 | + case BatteryStatus.Full: |
| 72 | + button.Text += "Full"; |
| 73 | + break; |
| 74 | + case BatteryStatus.Unknown: |
| 75 | + default: |
| 76 | + button.Text += "Unknown"; |
| 77 | + break; |
| 78 | + } |
| 79 | + }; |
| 80 | + stack.Children.Add(button); |
16 | 81 |
|
17 |
| - var speak = new Button { |
18 |
| - Text = "Hello, Forms !", |
19 |
| - VerticalOptions = LayoutOptions.CenterAndExpand, |
20 |
| - HorizontalOptions = LayoutOptions.CenterAndExpand, |
21 |
| - }; |
22 |
| - speak.Clicked += (sender, e) => { |
23 |
| - DependencyService.Get<ITextToSpeech>().Speak("Hello from Xamarin Forms"); |
24 |
| - }; |
25 |
| - stack.Children.Add(speak); |
| 82 | + // Device Orientation |
| 83 | + var orient = new Button |
| 84 | + { |
| 85 | + Text = "Get Orientation", |
| 86 | + VerticalOptions = LayoutOptions.CenterAndExpand, |
| 87 | + HorizontalOptions = LayoutOptions.CenterAndExpand, |
| 88 | + }; |
| 89 | + orient.Clicked += (sender, e) => |
| 90 | + { |
| 91 | + var orientation = DependencyService.Get<IDeviceOrientation>().GetOrientation(); |
| 92 | + switch (orientation) |
| 93 | + { |
| 94 | + case DeviceOrientations.Undefined: |
| 95 | + orient.Text = "Undefined"; |
| 96 | + break; |
| 97 | + case DeviceOrientations.Landscape: |
| 98 | + orient.Text = "Landscape"; |
| 99 | + break; |
| 100 | + case DeviceOrientations.Portrait: |
| 101 | + orient.Text = "Portrait"; |
| 102 | + break; |
| 103 | + } |
| 104 | + }; |
| 105 | + stack.Children.Add(orient); |
26 | 106 |
|
27 |
| - var button = new Button { |
28 |
| - Text = "Click for battery info", |
29 |
| - VerticalOptions = LayoutOptions.CenterAndExpand, |
30 |
| - HorizontalOptions = LayoutOptions.CenterAndExpand, |
31 |
| - }; |
32 |
| - button.Clicked += (sender, e) => { |
33 |
| - var bat = DependencyService.Get<IBattery>(); |
| 107 | + // Picture Picker |
| 108 | + Button pickPictureButton = new Button |
| 109 | + { |
| 110 | + Text = "Pick Photo", |
| 111 | + VerticalOptions = LayoutOptions.CenterAndExpand, |
| 112 | + HorizontalOptions = LayoutOptions.CenterAndExpand |
| 113 | + }; |
| 114 | + stack.Children.Add(pickPictureButton); |
34 | 115 |
|
35 |
| - switch (bat.PowerSource){ |
36 |
| - case PowerSource.Battery: |
37 |
| - button.Text = "Battery - "; |
38 |
| - break; |
39 |
| - case PowerSource.Ac: |
40 |
| - button.Text = "AC - "; |
41 |
| - break; |
42 |
| - case PowerSource.Usb: |
43 |
| - button.Text = "USB - "; |
44 |
| - break; |
45 |
| - case PowerSource.Wireless: |
46 |
| - button.Text = "Wireless - "; |
47 |
| - break; |
48 |
| - case PowerSource.Other: |
49 |
| - default: |
50 |
| - button.Text = "Unknown - "; |
51 |
| - break; |
52 |
| - } |
53 |
| - switch (bat.Status){ |
54 |
| - case BatteryStatus.Charging: |
55 |
| - button.Text += "Charging"; |
56 |
| - break; |
57 |
| - case BatteryStatus.Discharging: |
58 |
| - button.Text += "Discharging"; |
59 |
| - break; |
60 |
| - case BatteryStatus.NotCharging: |
61 |
| - button.Text += "Not Charging"; |
62 |
| - break; |
63 |
| - case BatteryStatus.Full: |
64 |
| - button.Text += "Full"; |
65 |
| - break; |
66 |
| - case BatteryStatus.Unknown: |
67 |
| - default: |
68 |
| - button.Text += "Unknown"; |
69 |
| - break; |
70 |
| - } |
71 |
| - }; |
72 |
| - stack.Children.Add (button); |
73 |
| - var orient = new Button { |
74 |
| - Text = "Get Orientation", |
75 |
| - VerticalOptions = LayoutOptions.CenterAndExpand, |
76 |
| - HorizontalOptions = LayoutOptions.CenterAndExpand, |
77 |
| - }; |
78 |
| - orient.Clicked += (sender, e) => { |
79 |
| - var orientation = DependencyService.Get<IDeviceOrientation>().GetOrientation(); |
80 |
| - switch(orientation){ |
81 |
| - case DeviceOrientations.Undefined: |
82 |
| - orient.Text = "Undefined"; |
83 |
| - break; |
84 |
| - case DeviceOrientations.Landscape: |
85 |
| - orient.Text = "Landscape"; |
86 |
| - break; |
87 |
| - case DeviceOrientations.Portrait: |
88 |
| - orient.Text = "Portrait"; |
89 |
| - break; |
90 |
| - } |
91 |
| - }; |
92 |
| - stack.Children.Add (orient); |
93 |
| - // The root page of your application |
| 116 | + pickPictureButton.Clicked += async (sender, e) => |
| 117 | + { |
| 118 | + pickPictureButton.IsEnabled = false; |
| 119 | + Stream stream = await DependencyService.Get<IPicturePicker>().GetImageStreamAsync(); |
| 120 | + // pickPictureButton.IsEnabled = true; |
94 | 121 |
|
95 |
| - } |
| 122 | + if (stream != null) |
| 123 | + { |
| 124 | + Image image = new Image |
| 125 | + { |
| 126 | + Source = ImageSource.FromStream(() => stream), |
| 127 | + BackgroundColor = Color.Gray |
| 128 | + }; |
| 129 | + TapGestureRecognizer recognizer = new TapGestureRecognizer(); |
| 130 | + recognizer.Tapped += (sender2, args) => |
| 131 | + { |
| 132 | + (MainPage as ContentPage).Content = stack; |
| 133 | + pickPictureButton.IsEnabled = true; |
| 134 | + }; |
| 135 | + image.GestureRecognizers.Add(recognizer); |
| 136 | + (MainPage as ContentPage).Content = image; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + pickPictureButton.IsEnabled = true; |
| 141 | + } |
| 142 | + }; |
| 143 | + } |
96 | 144 |
|
97 |
| - protected override void OnStart () |
98 |
| - { |
99 |
| - // Handle when your app starts |
100 |
| - } |
| 145 | + protected override void OnStart() |
| 146 | + { |
| 147 | + // Handle when your app starts |
| 148 | + } |
101 | 149 |
|
102 |
| - protected override void OnSleep () |
103 |
| - { |
104 |
| - // Handle when your app sleeps |
105 |
| - } |
| 150 | + protected override void OnSleep() |
| 151 | + { |
| 152 | + // Handle when your app sleeps |
| 153 | + } |
106 | 154 |
|
107 |
| - protected override void OnResume () |
108 |
| - { |
109 |
| - // Handle when your app resumes |
110 |
| - } |
111 |
| - } |
| 155 | + protected override void OnResume() |
| 156 | + { |
| 157 | + // Handle when your app resumes |
| 158 | + } |
| 159 | + } |
112 | 160 | }
|
113 | 161 |
|
0 commit comments