Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XAML:
- <!-- Exercise-sivu, joka hyödyntää ListViewin kautta WeatherViewModelia -->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise6"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:local_root="using:UWPCourseApp2022"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="50"/>
- <RowDefinition Height="*" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
- <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
- <ListView.ItemTemplate>
- <DataTemplate x:DataType="local_root:Weather">
- <StackPanel Orientation="Horizontal" Margin="6">
- <SymbolIcon Symbol="Audio" Margin="0,0,12,0"/>
- <StackPanel>
- <TextBlock Text="{x:Bind Summary}"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </Grid>
- </Page>
- <!-- Exercise Page, versio 2, yksityiskohtasivu -->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise6"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:local_root="using:UWPCourseApp2022"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="50"/>
- <RowDefinition Height="*" />
- <RowDefinition Height="150" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
- <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
- <ListView.ItemTemplate>
- <DataTemplate x:DataType="local_root:Weather">
- <StackPanel Orientation="Horizontal" Margin="6">
- <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
- <StackPanel>
- <TextBlock Text="{x:Bind Summary}"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <StackPanel Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}" Margin="0,24,0,0">
- <TextBlock FontWeight="Bold" Text="{Binding Location}"/>
- <TextBlock Text="{Binding Snow}"/>
- <TextBlock Text="{Binding Rain}"/>
- <TextBlock Text="{Binding Wind}"/>
- </StackPanel>
- </Grid>
- </Page>
- <!-- Exercise, Page , versio 3, lisätty Binding Description-->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise6"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:local_root="using:UWPCourseApp2022"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="50"/>
- <RowDefinition Height="*" />
- <RowDefinition Height="280" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0" Margin="20" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
- <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
- <ListView.ItemTemplate>
- <DataTemplate x:DataType="local_root:Weather">
- <StackPanel Orientation="Horizontal" Margin="6">
- <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
- <StackPanel>
- <TextBlock Text="{x:Bind Summary}"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <StackPanel Background="LightCyan" Margin="20" Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}">
- <TextBlock Margin="10" FontSize="20" FontWeight="Bold" Text="{Binding Location}"/>
- <TextBlock Margin="10" FontSize="18" Text="{Binding Snow}"/>
- <TextBlock Margin="10" FontSize="18" Text="{Binding Rain}"/>
- <TextBlock Margin="10" FontSize="18" Text="{Binding Wind}"/>
- <TextBlock Margin="10" FontStyle="Italic" FontSize="18" Text="{Binding Description}"/>
- </StackPanel>
- </Grid>
- </Page>
- <!-- Exercise Page, versio 4, StaticResourcella Margin, ei tarvitse copypastettaa samaa numeroa jokaiseen Marginiin-->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise6"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:local_root="using:UWPCourseApp2022"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Page.Resources>
- <Thickness x:Key="CommonMargin">10</Thickness>
- <Thickness x:Key="BasicMargin">20</Thickness>
- </Page.Resources>
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="50"/>
- <RowDefinition Height="*" />
- <RowDefinition Height="280" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0" Margin="{StaticResource BasicMargin}" FontSize="40" FontWeight="Bold">Päivän säätiedot!</TextBlock>
- <ListView Grid.Row="1" x:Name="weathersListView" ItemsSource="{x:Bind ViewModel.Weathers}">
- <ListView.ItemTemplate>
- <DataTemplate x:DataType="local_root:Weather">
- <StackPanel Orientation="Horizontal" Margin="6">
- <SymbolIcon Symbol="Street" Margin="0,0,12,0"/>
- <StackPanel>
- <TextBlock Text="{x:Bind Summary}"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <StackPanel Background="LightCyan" Margin="{StaticResource BasicMargin}" Grid.Row="2" DataContext="{Binding SelectedItem, ElementName=weathersListView}">
- <TextBlock Margin="{StaticResource CommonMargin}" FontSize="20" FontWeight="Bold" Text="{Binding Location}"/>
- <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Snow}"/>
- <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Rain}"/>
- <TextBlock Margin="{StaticResource CommonMargin}" FontSize="18" Text="{Binding Wind}"/>
- <TextBlock Margin="{StaticResource CommonMargin}" FontStyle="Italic" FontSize="18" Text="{Binding Description}"/>
- </StackPanel>
- </Grid>
- </Page>
- <!-- Ylävalikko-esimerkki. Ota Grid.Row="0" pois jos ohjelmasi ei käytä Gridiä.-->
- <!-- Ylävalikko START -->
- <MenuBar Grid.Row="0" VerticalAlignment="Top">
- <MenuBarItem Title="File">
- <MenuFlyoutItem Text="Open"
- Icon="OpenFile">
- </MenuFlyoutItem>
- <MenuFlyoutSeparator />
- <MenuFlyoutItem Text="Save"
- Icon="Save">
- </MenuFlyoutItem>
- <MenuFlyoutSubItem Text="Demo">
- <MenuFlyoutItem Text="A" Click="MenuFlyoutItem_Click"/>
- <MenuFlyoutItem Text="B" />
- <MenuFlyoutItem Text="C" />
- </MenuFlyoutSubItem>
- </MenuBarItem>
- <MenuBarItem Title="Help">
- <MenuFlyoutItem Text="About" />
- </MenuBarItem>
- </MenuBar>
- <!-- Ylävalikko END -->
- // Event handler:
- private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
- {
- // Initialize a new text for message dialog
- string message = "Onneksi olkoon, olet löytänyt äärimmäisen hienosti piilotetun valikkopainikkeen! Hyvä!";
- // Initialize a new MessageDialog instance
- MessageDialog messageDialog = new MessageDialog(message, "Hienosti löydetty!");
- // Display the message dialog
- _ = messageDialog.ShowAsync();
- }
- C#:
- // Data Bindingin data-luokka
- public class Weather
- {
- public string Location { get; set; }
- public double Snow { get; set; }
- public double Rain { get; set; }
- public double Wind { get; set; }
- public Weather()
- {
- Location = "Ei nimeä";
- Snow = 0.0;
- Rain = 0.0;
- Wind = 0.0;
- }
- public String Summary
- {
- get
- {
- return Location + ": " + Snow + " cm";
- }
- }
- }
- // ViewModel, eli luokka jossa varsinainen data haetaan, hyödyntää Weather-luokkaa
- public class WeatherViewModel
- {
- // HUOM: tyyppinä on ObservableCollection, joka automaattisesti
- // huolehtii Data Bindingin vaatimasta
- // PropertyChanged-ominaisuudesta!
- private ObservableCollection<Weather> weathers = new ObservableCollection<Weather>();
- // tehdään myös property ylläolevasta muuttujasta
- public ObservableCollection<Weather> Weathers { get { return this.weathers; } }
- public WeatherViewModel()
- {
- // asetetaan testimielessä muutama "Weather" -data listaan
- this.weathers.Add(new Weather()
- {
- Location = "Laboratorio",
- Snow = 14.6,
- Rain = 0.5,
- Wind = 3.4
- });
- this.weathers.Add(new Weather()
- {
- Location = "Kirjasto",
- Snow = 6.2,
- Rain = 1.2,
- Wind = 7.1
- });
- this.weathers.Add(new Weather()
- {
- Location = "Parkkipaikka",
- Snow = 54.6,
- Rain = 5.4,
- Wind = 7.5
- });
- }
- }
- // Page, esim. Exercise6, C#
- public sealed partial class Exercise6 : Page
- {
- public Exercise6()
- {
- this.InitializeComponent();
- this.ViewModel = new WeatherViewModel();
- }
- public WeatherViewModel ViewModel { get; set; }
- }
- // WeatherViewModel, versio 2, internet-data
- public class WeatherViewModel
- {
- // HUOM: tyyppinä on ObservableCollection, joka automaattisesti
- // huolehtii Data Bindingin vaatimasta
- // PropertyChanged-ominaisuudesta!
- private ObservableCollection<Weather> weathers = new ObservableCollection<Weather>();
- // tehdään myös property ylläolevasta muuttujasta
- public ObservableCollection<Weather> Weathers { get { return this.weathers; } }
- public WeatherViewModel()
- {
- String JSON_URL = "https://edu.frostbit.fi/api/weather/";
- // contents muuttujan sisältö voi tulla tiedostostakin,
- // ks. tiedoston lukeminen, esimerkit Moodlessa
- string contents;
- using (var wc = new System.Net.WebClient())
- contents = wc.DownloadString(JSON_URL);
- // on hyvä idea testata etukäteen vaikka näin että tulihan raaka-JSON perille asti
- //Debug.WriteLine(contents);
- // muutetaan taulukoksi
- JsonArray weatherItems = JsonArray.Parse(contents);
- // lisätään jokainen säätiedotus silmukassa observablecollectioniin
- foreach (JsonValue objvalue in weatherItems)
- {
- JsonObject obj = objvalue.GetObject() as JsonObject;
- this.weathers.Add(new Weather()
- {
- Location = obj.GetNamedString("location"),
- Snow = obj.GetNamedNumber("snow"),
- Rain = obj.GetNamedNumber("rain"),
- Wind = obj.GetNamedNumber("wind")
- });
- }
- /*
- this.weathers.Add(new Weather()
- {
- Location = "Koulun katolla",
- Snow = 156.2,
- Rain = 12.6,
- Wind = 11.9
- });*/
- }
- }
- // Weather, versio 2, Description -property
- public class Weather
- {
- public string Location { get; set; }
- public double Snow { get; set; }
- public double Rain { get; set; }
- public double Wind { get; set; }
- public Weather()
- {
- Location = "Ei nimeä";
- Snow = 0.0;
- Rain = 0.0;
- Wind = 0.0;
- }
- public String Summary
- {
- get
- {
- return Location + ": " + Snow + " senttimetriä";
- }
- }
- public String Description
- {
- get
- {
- String result = "";
- if(Wind == 0.0)
- {
- result = "Tyyntä.";
- }
- else if (Wind > 0 && Wind < 2)
- {
- result = "Tuulee hieman.";
- }
- else if (Wind >= 2 && Wind < 5)
- {
- result = "Siellä muuten TUULEE.";
- }
- else if(Wind >= 5)
- {
- result = "Ei mitään asiaa ulos.";
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement