Advertisement
tuomasvaltanen

Untitled

Mar 3rd, 2022 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. XAML:
  2. <!-- Exercise-sivu, joka hyödyntää ListViewin kautta WeatherViewModelia -->
  3. <Page
  4. x:Class="UWPCourseApp2022.Views.Exercise6"
  5. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:local="using:UWPCourseApp2022.Views"
  8. xmlns:local_root="using:UWPCourseApp2022"
  9. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  10. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  11. mc:Ignorable="d"
  12. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  13.  
  14. <Grid>
  15. <Grid.RowDefinitions>
  16. <RowDefinition Height="50"/>
  17. <RowDefinition Height="*" />
  18.  
  19. </Grid.RowDefinitions>
  20. <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
  21.  
  22. <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
  23. <ListView.ItemTemplate>
  24. <DataTemplate x:DataType="local_root:Weather">
  25. <StackPanel Orientation="Horizontal" Margin="6">
  26. <SymbolIcon Symbol="Audio" Margin="0,0,12,0"/>
  27. <StackPanel>
  28. <TextBlock Text="{x:Bind Summary}"/>
  29. </StackPanel>
  30. </StackPanel>
  31. </DataTemplate>
  32. </ListView.ItemTemplate>
  33. </ListView>
  34. </Grid>
  35. </Page>
  36.  
  37. <!-- Exercise Page, versio 2, yksityiskohtasivu -->
  38.  
  39. <Page
  40. x:Class="UWPCourseApp2022.Views.Exercise6"
  41. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  42. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  43. xmlns:local="using:UWPCourseApp2022.Views"
  44. xmlns:local_root="using:UWPCourseApp2022"
  45. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  46. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  47. mc:Ignorable="d"
  48. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  49.  
  50. <Grid>
  51. <Grid.RowDefinitions>
  52. <RowDefinition Height="50"/>
  53. <RowDefinition Height="*" />
  54. <RowDefinition Height="150" />
  55.  
  56. </Grid.RowDefinitions>
  57. <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
  58.  
  59. <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
  60. <ListView.ItemTemplate>
  61. <DataTemplate x:DataType="local_root:Weather">
  62. <StackPanel Orientation="Horizontal" Margin="6">
  63. <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
  64. <StackPanel>
  65. <TextBlock Text="{x:Bind Summary}"/>
  66. </StackPanel>
  67. </StackPanel>
  68. </DataTemplate>
  69. </ListView.ItemTemplate>
  70. </ListView>
  71.  
  72. <StackPanel Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}" Margin="0,24,0,0">
  73. <TextBlock FontWeight="Bold" Text="{Binding Location}"/>
  74. <TextBlock Text="{Binding Snow}"/>
  75. <TextBlock Text="{Binding Rain}"/>
  76. <TextBlock Text="{Binding Wind}"/>
  77. </StackPanel>
  78. </Grid>
  79. </Page>
  80.  
  81. <!-- Exercise, Page , versio 3, lisätty Binding Description-->
  82.  
  83. <Page
  84. x:Class="UWPCourseApp2022.Views.Exercise6"
  85. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  86. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  87. xmlns:local="using:UWPCourseApp2022.Views"
  88. xmlns:local_root="using:UWPCourseApp2022"
  89. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  90. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  91. mc:Ignorable="d"
  92. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  93.  
  94. <Grid>
  95. <Grid.RowDefinitions>
  96. <RowDefinition Height="50"/>
  97. <RowDefinition Height="*" />
  98. <RowDefinition Height="280" />
  99.  
  100. </Grid.RowDefinitions>
  101. <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
  102.  
  103. <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
  104. <ListView.ItemTemplate>
  105. <DataTemplate x:DataType="local_root:Weather">
  106. <StackPanel Orientation="Horizontal" Margin="6">
  107. <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
  108. <StackPanel>
  109. <TextBlock Text="{x:Bind Summary}"/>
  110. </StackPanel>
  111. </StackPanel>
  112. </DataTemplate>
  113. </ListView.ItemTemplate>
  114. </ListView>
  115.  
  116. <StackPanel Background="LightCyan" Margin="20" Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}">
  117. <TextBlock Margin="10" FontSize="20" FontWeight="Bold" Text="{Binding Location}"/>
  118. <TextBlock Margin="10" FontSize="18" Text="{Binding Snow}"/>
  119. <TextBlock Margin="10" FontSize="18" Text="{Binding Rain}"/>
  120. <TextBlock Margin="10" FontSize="18" Text="{Binding Wind}"/>
  121. <TextBlock Margin="10" FontStyle="Italic" FontSize="18" Text="{Binding Description}"/>
  122. </StackPanel>
  123. </Grid>
  124. </Page>
  125.  
  126. <!-- Exercise Page, versio 4, StaticResourcella Margin, ei tarvitse copypastettaa samaa numeroa jokaiseen Marginiin-->
  127.  
  128. <Page
  129. x:Class="UWPCourseApp2022.Views.Exercise6"
  130. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  131. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  132. xmlns:local="using:UWPCourseApp2022.Views"
  133. xmlns:local_root="using:UWPCourseApp2022"
  134. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  135. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  136. mc:Ignorable="d"
  137. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  138.  
  139. <Page.Resources>
  140. <Thickness x:Key="CommonMargin">10</Thickness>
  141. <Thickness x:Key="BasicMargin">20</Thickness>
  142. </Page.Resources>
  143.  
  144. <Grid>
  145. <Grid.RowDefinitions>
  146. <RowDefinition Height="50"/>
  147. <RowDefinition Height="*" />
  148. <RowDefinition Height="280" />
  149.  
  150. </Grid.RowDefinitions>
  151. <TextBlock Grid.Row="0" Margin="{StaticResource BasicMargin}" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
  152.  
  153. <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
  154. <ListView.ItemTemplate>
  155. <DataTemplate x:DataType="local_root:Weather">
  156. <StackPanel Orientation="Horizontal" Margin="6">
  157. <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
  158. <StackPanel>
  159. <TextBlock Text="{x:Bind Summary}"/>
  160. </StackPanel>
  161. </StackPanel>
  162. </DataTemplate>
  163. </ListView.ItemTemplate>
  164. </ListView>
  165.  
  166. <StackPanel Background="LightCyan" Margin="{StaticResource BasicMargin}" Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}">
  167. <TextBlock Margin="{StaticResource CommonMargin}" FontSize="20" FontWeight="Bold" Text="{Binding Location}"/>
  168. <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Snow}"/>
  169. <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Rain}"/>
  170. <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Wind}"/>
  171. <TextBlock Margin="{StaticResource CommonMargin}" FontStyle="Italic" FontSize="18" Text="{Binding Description}"/>
  172. </StackPanel>
  173. </Grid>
  174. </Page>
  175.  
  176. <!-- Ylävalikko-esimerkki. Ota Grid.Row="0" pois jos ohjelmasi ei käytä Gridiä.-->
  177. <!-- Ylävalikko START -->
  178.  
  179. <MenuBar Grid.Row="0" VerticalAlignment="Top">
  180. <MenuBarItem Title="File">
  181. <MenuFlyoutItem Text="Open"
  182. Icon="OpenFile">
  183. </MenuFlyoutItem>
  184. <MenuFlyoutSeparator />
  185. <MenuFlyoutItem Text="Save"
  186. Icon="Save">
  187. </MenuFlyoutItem>
  188. <MenuFlyoutSubItem Text="Demo">
  189. <MenuFlyoutItem Text="A" Click="MenuFlyoutItem_Click"/>
  190. <MenuFlyoutItem Text="B" />
  191. <MenuFlyoutItem Text="C" />
  192. </MenuFlyoutSubItem>
  193. </MenuBarItem>
  194. <MenuBarItem Title="Help">
  195. <MenuFlyoutItem Text="About" />
  196. </MenuBarItem>
  197. </MenuBar>
  198.  
  199. <!-- Ylävalikko END -->
  200.  
  201. // Event handler:
  202. private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
  203. {
  204. // Initialize a new text for message dialog
  205. string message = "Onneksi olkoon, olet löytänyt äärimmäisen hienosti piilotetun valikkopainikkeen! Hyvä!";
  206.  
  207. // Initialize a new MessageDialog instance
  208. MessageDialog messageDialog = new MessageDialog(message, "Hienosti löydetty!");
  209.  
  210. // Display the message dialog
  211. _ = messageDialog.ShowAsync();
  212. }
  213.  
  214. C#:
  215. // Data Bindingin data-luokka
  216.  
  217. public class Weather
  218. {
  219. public string Location { get; set; }
  220. public double Snow { get; set; }
  221. public double Rain { get; set; }
  222. public double Wind { get; set; }
  223.  
  224. public Weather()
  225. {
  226. Location = "Ei nimeä";
  227. Snow = 0.0;
  228. Rain = 0.0;
  229. Wind = 0.0;
  230. }
  231.  
  232. public String Summary
  233. {
  234. get
  235. {
  236. return Location + ": " + Snow + " cm";
  237. }
  238. }
  239. }
  240.  
  241. // ViewModel, eli luokka jossa varsinainen data haetaan, hyödyntää Weather-luokkaa
  242.  
  243. public class WeatherViewModel
  244. {
  245. // HUOM: tyyppinä on ObservableCollection, joka automaattisesti
  246. // huolehtii Data Bindingin vaatimasta
  247. // PropertyChanged-ominaisuudesta!
  248. private ObservableCollection<Weather> weathers = new ObservableCollection<Weather>();
  249.  
  250. // tehdään myös property ylläolevasta muuttujasta
  251. public ObservableCollection<Weather> Weathers { get { return this.weathers; } }
  252.  
  253. public WeatherViewModel()
  254. {
  255. // asetetaan testimielessä muutama "Weather" -data listaan
  256. this.weathers.Add(new Weather()
  257. {
  258. Location = "Laboratorio",
  259. Snow = 14.6,
  260. Rain = 0.5,
  261. Wind = 3.4
  262. });
  263.  
  264. this.weathers.Add(new Weather()
  265. {
  266. Location = "Kirjasto",
  267. Snow = 6.2,
  268. Rain = 1.2,
  269. Wind = 7.1
  270. });
  271.  
  272. this.weathers.Add(new Weather()
  273. {
  274. Location = "Parkkipaikka",
  275. Snow = 54.6,
  276. Rain = 5.4,
  277. Wind = 7.5
  278. });
  279. }
  280. }
  281.  
  282. // Page, esim. Exercise6, C#
  283.  
  284. public sealed partial class Exercise6 : Page
  285. {
  286. public Exercise6()
  287. {
  288. this.InitializeComponent();
  289. this.ViewModel = new WeatherViewModel();
  290. }
  291.  
  292. public WeatherViewModel ViewModel { get; set; }
  293. }
  294.  
  295. // WeatherViewModel, versio 2, internet-data
  296.  
  297. public class WeatherViewModel
  298. {
  299. // HUOM: tyyppinä on ObservableCollection, joka automaattisesti
  300. // huolehtii Data Bindingin vaatimasta
  301. // PropertyChanged-ominaisuudesta!
  302. private ObservableCollection<Weather> weathers = new ObservableCollection<Weather>();
  303.  
  304. // tehdään myös property ylläolevasta muuttujasta
  305. public ObservableCollection<Weather> Weathers { get { return this.weathers; } }
  306.  
  307. public WeatherViewModel()
  308. {
  309. String JSON_URL = "https://edu.frostbit.fi/api/weather/";
  310.  
  311. // contents muuttujan sisältö voi tulla tiedostostakin,
  312. // ks. tiedoston lukeminen, esimerkit Moodlessa
  313. string contents;
  314. using (var wc = new System.Net.WebClient())
  315. contents = wc.DownloadString(JSON_URL);
  316.  
  317.  
  318. // on hyvä idea testata etukäteen vaikka näin että tulihan raaka-JSON perille asti
  319. //Debug.WriteLine(contents);
  320.  
  321. // muutetaan taulukoksi
  322. JsonArray weatherItems = JsonArray.Parse(contents);
  323.  
  324. // lisätään jokainen säätiedotus silmukassa observablecollectioniin
  325. foreach (JsonValue objvalue in weatherItems)
  326. {
  327. JsonObject obj = objvalue.GetObject() as JsonObject;
  328.  
  329. this.weathers.Add(new Weather()
  330. {
  331. Location = obj.GetNamedString("location"),
  332. Snow = obj.GetNamedNumber("snow"),
  333. Rain = obj.GetNamedNumber("rain"),
  334. Wind = obj.GetNamedNumber("wind")
  335. });
  336. }
  337.  
  338.  
  339. /*
  340. this.weathers.Add(new Weather()
  341. {
  342. Location = "Koulun katolla",
  343. Snow = 156.2,
  344. Rain = 12.6,
  345. Wind = 11.9
  346. });*/
  347. }
  348. }
  349.  
  350.  
  351. // Weather, versio 2, Description -property
  352.  
  353. public class Weather
  354. {
  355. public string Location { get; set; }
  356. public double Snow { get; set; }
  357. public double Rain { get; set; }
  358. public double Wind { get; set; }
  359.  
  360. public Weather()
  361. {
  362. Location = "Ei nimeä";
  363. Snow = 0.0;
  364. Rain = 0.0;
  365. Wind = 0.0;
  366. }
  367.  
  368. public String Summary
  369. {
  370. get
  371. {
  372. return Location + ": " + Snow + " senttimetriä";
  373. }
  374. }
  375.  
  376. public String Description
  377. {
  378. get
  379. {
  380. String result = "";
  381.  
  382. if(Wind == 0.0)
  383. {
  384. result = "Tyyntä.";
  385. }
  386. else if (Wind > 0 && Wind < 2)
  387. {
  388. result = "Tuulee hieman.";
  389. }
  390. else if (Wind >= 2 && Wind < 5)
  391. {
  392. result = "Siellä muuten TUULEE.";
  393. }
  394. else if(Wind >= 5)
  395. {
  396. result = "Ei mitään asiaa ulos.";
  397. }
  398.  
  399. return result;
  400. }
  401. }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement