Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XAML:
- <ComboBox Margin="20" FontSize="20" SelectedItem="{x:Bind ComboBoxNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
- <ComboBoxItem Tag="1">Yksi</ComboBoxItem>
- <ComboBoxItem Tag="2">Kaksi</ComboBoxItem>
- <ComboBoxItem Tag="3">Kolme</ComboBoxItem>
- <ComboBoxItem Tag="4">Neljä</ComboBoxItem>
- </ComboBox>
- <TextBlock Text="{x:Bind ComboToNumber(ComboBoxNumber.Tag), Mode=OneWay}" FontSize="20" Margin="20">TESTATAAN</TextBlock>
- <!-- DATA BINDING , huomaa local_root -apumuuttuja Pagen sisällä -->
- <Page
- x:Class="UWPCourseApp2021.Views.Exercise4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2021.Views"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local1="using:UWPCourseApp2021"
- mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <ScrollViewer>
- <StackPanel Orientation="Vertical">
- <TextBlock FontSize="80" Margin="20">Päivän sää!</TextBlock>
- <ListView x:Name="weatherListView" ItemsSource="{x:Bind ViewModel.Weathers}">
- <ListView.ItemTemplate>
- <DataTemplate x:DataType="local1:Weather">
- <StackPanel Orientation="Horizontal" Margin="10">
- <SymbolIcon Symbol="Street" Margin="10"/>
- <StackPanel Margin="10">
- <TextBlock FontSize="20" FontWeight="Bold" Text="{x:Bind Summary}"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <StackPanel Margin="20" DataContext="{Binding SelectedItem, ElementName=weatherListView}">
- <TextBlock FontSize="30" FontWeight="Bold" Text="{Binding Location}"/>
- <TextBlock FontSize="30" FontWeight="Bold" Text="{Binding Description}"></TextBlock>
- </StackPanel>
- </StackPanel>
- </ScrollViewer>
- </Page>
- C:
- String jokeApi = "https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit";
- String contents;
- using (var wc = new System.Net.WebClient())
- contents = wc.DownloadString(jokeApi);
- JsonObject obj = JsonObject.Parse(contents);
- String joke = "";
- // pidetään huoli ettei ohjelma tilttaa jos vitsitekstiä ei löydy
- try
- {
- // versio 1, vitsi on joke-kentässä
- if(obj.ContainsKey("joke"))
- {
- // tässä tapauksessa GetString hakee tekstiarvon, ToString
- // hakee raakamuodossa jolloin ylimääräisiä merkkejä tulee mukaan
- joke = obj.GetNamedValue("joke").GetString();
- }
- else
- {
- // versio 2, vitsti koostuu setup- ja delivery-osasta
- // yhdistetään yhdeksi vitsiksi
- if(obj.ContainsKey("setup") && obj.ContainsKey("delivery"))
- {
- joke = joke + obj.GetNamedValue("setup").GetString() + "\n";
- joke = joke + " - " + obj.GetNamedValue("delivery").GetString();
- }
- }
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- joke = "Virhe.";
- }
- RandomJoke = joke;
- OnPropertyChanged("RandomJoke");
- // function binding
- private ComboBoxItem comboBoxNumber;
- public ComboBoxItem ComboBoxNumber
- {
- get
- {
- return comboBoxNumber;
- }
- set
- {
- comboBoxNumber = value;
- OnPropertyChanged("ComboBoxNumber");
- }
- }
- // apumetodi, jota käytetään function binding
- public String ComboToNumber(object orig)
- {
- String result = orig as String;
- int num = 0;
- // muutetaan Tagissa oleva numero kokonaisluvuksi
- Int32.TryParse(result, out num);
- // kerrotaan kahdella ja muutetaan tekstiksi
- int result_number = num * 2;
- result = result_number.ToString();
- // palautetaan muutettu arvo
- return result;
- }
- // WEATHER-LUOKKA DATA BINDAUSTA VARTEN
- // muista laittaa public luokan näkyvyydeksi jotta sitä voi käyttää
- // Data bindingissa!
- 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 = "Virheellinen säätieto.";
- }
- public String Summary
- {
- get
- {
- return $"{this.Location}\nLumi: {this.Snow} cm\nSade: {this.Rain} mm\nTuuli: {this.Wind} m/s ";
- }
- }
- // Property, joka arvioi sanallisesti millainen sää on
- // käytetään vain silloin kun näytetään yksityiskohta yksittäisestä säätiedotteesta
- public String Description
- {
- get
- {
- String result = "";
- if(Snow > 0)
- {
- result = "Lunta. ";
- }
- if(Rain > 0)
- {
- result = "Sataa. ";
- }
- if (Snow > 0 && Rain > 0)
- {
- result = "Räntää. ";
- }
- if(Wind > 0 && Wind < 3)
- {
- result = result + "Tuulee hieman.";
- }
- else if(Wind >= 3)
- {
- result = result + "Tuulee paljon.";
- }
- return result;
- }
- }
- }
- // View MOdel
- 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; } }
- // lisätään varsinainen data ViewModeliin
- public WeatherViewModel()
- {
- // rajapinnan osoite
- String url = "https://edu.frostbit.fi/api/weather/";
- string contents = "";
- // jos joku menee pieleen säätietojen osalta, ohjelma ei tilttaa kun käytämme try/catch
- try
- {
- using (var wc = new System.Net.WebClient())
- contents = wc.DownloadString(url);
- // muutetaan raaka JSON taulukoksi/listaksi
- JsonArray weatherItems = JsonArray.Parse(contents);
- // käydään jokainen säätiedote listassa läpi (yhteensä 9, ks. raaka data)
- foreach (JsonValue objvalue in weatherItems)
- {
- // muutetaan yhden säätiedotuksen JSON objektiksi
- JsonObject obj = objvalue.GetObject() as JsonObject;
- // lisätään säätiedote ja haetaan yksittäiset tiedot objektista
- this.weathers.Add(new Weather()
- {
- Location = obj.GetNamedString("location"),
- Snow = obj.GetNamedNumber("snow"),
- Rain = obj.GetNamedNumber("rain"),
- Wind = obj.GetNamedNumber("wind")
- });
- }
- }
- catch(Exception ex)
- {
- Debug.WriteLine(ex.Message);
- }
- //Debug.WriteLine(contents);
- /*
- // lisätään testidataa
- this.weathers.Add(new Weather()
- {
- Location = "Laboratorio",
- Snow = 30.0,
- Rain = 0.5,
- Wind = 4.5
- });
- this.weathers.Add(new Weather()
- {
- Location = "Kaupunki",
- Snow = 50.0,
- Rain = 1.2,
- Wind = 2.7
- });
- this.weathers.Add(new Weather()
- {
- Location = "Kellari",
- Snow = 45.0,
- Rain = 0.1,
- Wind = 3.2
- });
- */
- }
- }
- // ViewModelin kytkeminen Propertynn
- public Exercise4()
- {
- this.InitializeComponent();
- this.ViewModel = new WeatherViewModel();
- }
- public WeatherViewModel ViewModel { get; set; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement