Advertisement
tuomasvaltanen

Untitled

Mar 30th, 2021 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. XAML:
  2.  
  3. <ComboBox Margin="20" FontSize="20" SelectedItem="{x:Bind ComboBoxNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
  4. <ComboBoxItem Tag="1">Yksi</ComboBoxItem>
  5. <ComboBoxItem Tag="2">Kaksi</ComboBoxItem>
  6. <ComboBoxItem Tag="3">Kolme</ComboBoxItem>
  7. <ComboBoxItem Tag="4">Neljä</ComboBoxItem>
  8. </ComboBox>
  9.  
  10. <TextBlock Text="{x:Bind ComboToNumber(ComboBoxNumber.Tag), Mode=OneWay}" FontSize="20" Margin="20">TESTATAAN</TextBlock>
  11.  
  12. <!-- DATA BINDING , huomaa local_root -apumuuttuja Pagen sisällä -->
  13. <Page
  14. x:Class="UWPCourseApp2021.Views.Exercise4"
  15. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  16. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  17. xmlns:local="using:UWPCourseApp2021.Views"
  18.  
  19. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  20. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local1="using:UWPCourseApp2021"
  21. mc:Ignorable="d"
  22. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  23.  
  24. <ScrollViewer>
  25. <StackPanel Orientation="Vertical">
  26. <TextBlock FontSize="80" Margin="20">Päivän sää!</TextBlock>
  27.  
  28. <ListView x:Name="weatherListView" ItemsSource="{x:Bind ViewModel.Weathers}">
  29. <ListView.ItemTemplate>
  30. <DataTemplate x:DataType="local1:Weather">
  31. <StackPanel Orientation="Horizontal" Margin="10">
  32. <SymbolIcon Symbol="Street" Margin="10"/>
  33. <StackPanel Margin="10">
  34. <TextBlock FontSize="20" FontWeight="Bold" Text="{x:Bind Summary}"/>
  35. </StackPanel>
  36. </StackPanel>
  37. </DataTemplate>
  38. </ListView.ItemTemplate>
  39. </ListView>
  40.  
  41. <StackPanel Margin="20" DataContext="{Binding SelectedItem, ElementName=weatherListView}">
  42. <TextBlock FontSize="30" FontWeight="Bold" Text="{Binding Location}"/>
  43. <TextBlock FontSize="30" FontWeight="Bold" Text="{Binding Description}"></TextBlock>
  44.  
  45. </StackPanel>
  46.  
  47. </StackPanel>
  48. </ScrollViewer>
  49. </Page>
  50.  
  51.  
  52.  
  53. C:
  54.  
  55. String jokeApi = "https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit";
  56.  
  57. String contents;
  58. using (var wc = new System.Net.WebClient())
  59. contents = wc.DownloadString(jokeApi);
  60.  
  61. JsonObject obj = JsonObject.Parse(contents);
  62.  
  63. String joke = "";
  64.  
  65. // pidetään huoli ettei ohjelma tilttaa jos vitsitekstiä ei löydy
  66. try
  67. {
  68. // versio 1, vitsi on joke-kentässä
  69. if(obj.ContainsKey("joke"))
  70. {
  71. // tässä tapauksessa GetString hakee tekstiarvon, ToString
  72. // hakee raakamuodossa jolloin ylimääräisiä merkkejä tulee mukaan
  73. joke = obj.GetNamedValue("joke").GetString();
  74. }
  75. else
  76. {
  77. // versio 2, vitsti koostuu setup- ja delivery-osasta
  78. // yhdistetään yhdeksi vitsiksi
  79. if(obj.ContainsKey("setup") && obj.ContainsKey("delivery"))
  80. {
  81. joke = joke + obj.GetNamedValue("setup").GetString() + "\n";
  82. joke = joke + " - " + obj.GetNamedValue("delivery").GetString();
  83. }
  84. }
  85.  
  86.  
  87. }
  88. catch (Exception ex)
  89. {
  90. Debug.WriteLine(ex.Message);
  91. joke = "Virhe.";
  92. }
  93.  
  94. RandomJoke = joke;
  95.  
  96. OnPropertyChanged("RandomJoke");
  97.  
  98.  
  99. // function binding
  100. private ComboBoxItem comboBoxNumber;
  101.  
  102. public ComboBoxItem ComboBoxNumber
  103. {
  104. get
  105. {
  106. return comboBoxNumber;
  107. }
  108.  
  109. set
  110. {
  111. comboBoxNumber = value;
  112. OnPropertyChanged("ComboBoxNumber");
  113. }
  114. }
  115.  
  116. // apumetodi, jota käytetään function binding
  117. public String ComboToNumber(object orig)
  118. {
  119. String result = orig as String;
  120. int num = 0;
  121.  
  122. // muutetaan Tagissa oleva numero kokonaisluvuksi
  123. Int32.TryParse(result, out num);
  124.  
  125. // kerrotaan kahdella ja muutetaan tekstiksi
  126. int result_number = num * 2;
  127. result = result_number.ToString();
  128.  
  129. // palautetaan muutettu arvo
  130. return result;
  131. }
  132.  
  133.  
  134. // WEATHER-LUOKKA DATA BINDAUSTA VARTEN
  135.  
  136. // muista laittaa public luokan näkyvyydeksi jotta sitä voi käyttää
  137. // Data bindingissa!
  138. public class Weather
  139. {
  140. public String Location { get; set; }
  141. public double Snow { get; set; }
  142. public double Rain { get; set; }
  143. public double Wind { get; set; }
  144.  
  145.  
  146. public Weather()
  147. {
  148. Location = "Virheellinen säätieto.";
  149. }
  150.  
  151. public String Summary
  152. {
  153. get
  154. {
  155. return $"{this.Location}\nLumi: {this.Snow} cm\nSade: {this.Rain} mm\nTuuli: {this.Wind} m/s ";
  156. }
  157. }
  158.  
  159. // Property, joka arvioi sanallisesti millainen sää on
  160. // käytetään vain silloin kun näytetään yksityiskohta yksittäisestä säätiedotteesta
  161. public String Description
  162. {
  163. get
  164. {
  165. String result = "";
  166.  
  167. if(Snow > 0)
  168. {
  169. result = "Lunta. ";
  170. }
  171.  
  172. if(Rain > 0)
  173. {
  174. result = "Sataa. ";
  175. }
  176.  
  177. if (Snow > 0 && Rain > 0)
  178. {
  179. result = "Räntää. ";
  180. }
  181.  
  182. if(Wind > 0 && Wind < 3)
  183. {
  184. result = result + "Tuulee hieman.";
  185. }
  186. else if(Wind >= 3)
  187. {
  188. result = result + "Tuulee paljon.";
  189. }
  190.  
  191. return result;
  192. }
  193. }
  194. }
  195.  
  196. // View MOdel
  197. public class WeatherViewModel
  198. {
  199. // HUOM: tyyppinä on ObservableCollection, joka automaattisesti
  200. // huolehtii Data Bindingin vaatimasta
  201. // PropertyChanged-ominaisuudesta!
  202. private ObservableCollection<Weather> weathers = new ObservableCollection<Weather>();
  203.  
  204. // tehdään myös property ylläolevasta muuttujasta
  205. public ObservableCollection<Weather> Weathers { get { return this.weathers; } }
  206.  
  207. // lisätään varsinainen data ViewModeliin
  208. public WeatherViewModel()
  209. {
  210. // rajapinnan osoite
  211. String url = "https://edu.frostbit.fi/api/weather/";
  212.  
  213. string contents = "";
  214.  
  215. // jos joku menee pieleen säätietojen osalta, ohjelma ei tilttaa kun käytämme try/catch
  216. try
  217. {
  218. using (var wc = new System.Net.WebClient())
  219. contents = wc.DownloadString(url);
  220.  
  221. // muutetaan raaka JSON taulukoksi/listaksi
  222. JsonArray weatherItems = JsonArray.Parse(contents);
  223.  
  224. // käydään jokainen säätiedote listassa läpi (yhteensä 9, ks. raaka data)
  225. foreach (JsonValue objvalue in weatherItems)
  226. {
  227. // muutetaan yhden säätiedotuksen JSON objektiksi
  228. JsonObject obj = objvalue.GetObject() as JsonObject;
  229.  
  230. // lisätään säätiedote ja haetaan yksittäiset tiedot objektista
  231. this.weathers.Add(new Weather()
  232. {
  233. Location = obj.GetNamedString("location"),
  234. Snow = obj.GetNamedNumber("snow"),
  235. Rain = obj.GetNamedNumber("rain"),
  236. Wind = obj.GetNamedNumber("wind")
  237. });
  238. }
  239. }
  240. catch(Exception ex)
  241. {
  242. Debug.WriteLine(ex.Message);
  243. }
  244.  
  245. //Debug.WriteLine(contents);
  246.  
  247. /*
  248. // lisätään testidataa
  249. this.weathers.Add(new Weather()
  250. {
  251. Location = "Laboratorio",
  252. Snow = 30.0,
  253. Rain = 0.5,
  254. Wind = 4.5
  255. });
  256.  
  257. this.weathers.Add(new Weather()
  258. {
  259. Location = "Kaupunki",
  260. Snow = 50.0,
  261. Rain = 1.2,
  262. Wind = 2.7
  263. });
  264.  
  265. this.weathers.Add(new Weather()
  266. {
  267. Location = "Kellari",
  268. Snow = 45.0,
  269. Rain = 0.1,
  270. Wind = 3.2
  271. });
  272. */
  273. }
  274. }
  275.  
  276. // ViewModelin kytkeminen Propertynn
  277. public Exercise4()
  278. {
  279. this.InitializeComponent();
  280. this.ViewModel = new WeatherViewModel();
  281. }
  282.  
  283. public WeatherViewModel ViewModel { get; set; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement