Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XAML:
- <!-- Data binding XAMLissa, muista lisätä d:DataContext jos Page käyttää data bindingia! -->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise3"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- d:DataContext="{d:DesignInstance Type=local:Exercise3}"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel>
- <TextBlock Margin="20" FontSize="60">Exercise 3</TextBlock>
- <TextBlock TextWrapping="Wrap" Text="{x:Bind PersonalText, Mode=OneWay}" Margin="20" FontSize="40"></TextBlock>
- <Button Click="Button_Click">Muuta dataa</Button>
- <TextBox Text="{x:Bind CurrentTextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="20"></TextBox>
- <TextBlock Text="{x:Bind CurrentTextValue, Mode=OneWay}" Margin="20"></TextBlock>
- <ComboBox Margin="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}" Margin="20" FontSize="30"></TextBlock>
- </StackPanel>
- </Page>
- <!-- NuGetilla ladattu lisäkomponentti, RadialGauge -esimerkki-->
- <Page
- x:Class="UWPCourseApp2022.Views.Exercise1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:UWPCourseApp2022.Views"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel>
- <TextBlock Margin="20" FontSize="60">Exercise 1</TextBlock>
- <Button Click="Button_Click">Parametrin kanssa -> Exercise 2</Button>
- <Button Click="Button_Click_1">Arvo uusi nopeus</Button>
- <controls:RadialGauge Width="500" x:Name="RadialGaugeControl" Value="70" Minimum="0"
- Maximum="180" TickSpacing="20" ScaleWidth="26" Unit="Units" TickBrush="Gainsboro"
- ScaleTickBrush="{ThemeResource ApplicationPageBackgroundThemeBrush}"
- NeedleWidth="5" TickLength="18">
- </controls:RadialGauge>
- </StackPanel>
- </Page>
- C#:
- public sealed partial class Exercise3 : Page, INotifyPropertyChanged
- {
- public Exercise3()
- {
- PersonalText = "Data Binding sanoo että terve!";
- this.InitializeComponent();
- }
- public String PersonalText { get; set; }
- // koska tätä Propertya käytetään TwoWayBindingissa
- // on parempi laittaa "OnPropertyChanged"
- // set-metodin sisälle Propertyssä
- private String currentTextValue = "";
- public String CurrentTextValue
- {
- get
- {
- return currentTextValue;
- }
- set
- {
- currentTextValue = value;
- OnPropertyChanged("CurrentTextValue");
- }
- }
- // HUOM: MainPage tässä tapauksessa toteuttaa rajapinnan INotifyPropertyChanged
- // seuraavat rivit ovat sitä varten olemassa, että tehdyt muutokset ServiceNameText-
- // propertyyn voidaan päivittää XAMLiin
- public event PropertyChangedEventHandler PropertyChanged;
- private void OnPropertyChanged(string propertyName)
- {
- var eventHandler = this.PropertyChanged;
- if (eventHandler != null)
- eventHandler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- // vitsin lataaminen internetistä ja asettaminen Propertyyn
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- // Random rnd = new Random();
- // int randomNumber = rnd.Next(100);
- // PersonalText = "Muutettu! " + randomNumber;
- // haetaan uusi vitsi internetistä
- String url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&format=txt&type=single";
- // contents muuttujan sisältö voi tulla tiedostostakin,
- // ks. tiedoston lukeminen, esimerkit Moodlessa
- string contents;
- using (var wc = new System.Net.WebClient())
- contents = wc.DownloadString(url);
- // asetetaan ladattu vitsi Propertyyn, joka näyttää Data Bindingin avulla
- // sen XAMLissa!
- PersonalText = contents;
- OnPropertyChanged("PersonalText");
- }
- // JSON -versio samasta rajapinnasta
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- // Random rnd = new Random();
- // int randomNumber = rnd.Next(100);
- // PersonalText = "Muutettu! " + randomNumber;
- // haetaan uusi vitsi internetistä
- //String url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&format=txt&type=single";
- // haetaan JSON -muodossa data
- String url = "https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single";
- // contents muuttujan sisältö voi tulla tiedostostakin,
- // ks. tiedoston lukeminen, esimerkit Moodlessa
- string contents;
- using (var wc = new System.Net.WebClient())
- contents = wc.DownloadString(url);
- // muutetaan raaka JSON JsonObjectiksi
- JsonObject obj = JsonObject.Parse(contents);
- // haetaan JsonObjectista kenttä nimeltä "joke"
- String joke = obj.GetNamedString("joke");
- // asetetaan ladattu vitsi Propertyyn, joka näyttää Data Bindingin avulla
- // sen XAMLissa!
- PersonalText = joke;
- OnPropertyChanged("PersonalText");
- }
- // function binding esimerkki
- private ComboBoxItem comboBoxNumber;
- public ComboBoxItem ComboBoxNumber
- {
- get
- {
- return comboBoxNumber;
- }
- set
- {
- comboBoxNumber = value;
- OnPropertyChanged("ComboBoxNumber");
- }
- }
- public String ComboToNumber(object orig)
- {
- String number = orig as String;
- return number;
- }
- // RadialGaugen muutoskoodi:
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Random rnd = new Random();
- int randomNumber = rnd.Next(180);
- RadialGaugeControl.Value = randomNumber;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement