Advertisement
tuomasvaltanen

Untitled

Feb 14th, 2022 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. XAML:
  2. <!-- Data binding XAMLissa, muista lisätä d:DataContext jos Page käyttää data bindingia! -->
  3. <Page
  4. x:Class="UWPCourseApp2022.Views.Exercise3"
  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:d="http://schemas.microsoft.com/expression/blend/2008"
  9. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  10. mc:Ignorable="d"
  11. d:DataContext="{d:DesignInstance Type=local:Exercise3}"
  12. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  13.  
  14. <StackPanel>
  15. <TextBlock Margin="20" FontSize="60">Exercise 3</TextBlock>
  16.  
  17. <TextBlock TextWrapping="Wrap" Text="{x:Bind PersonalText, Mode=OneWay}" Margin="20" FontSize="40"></TextBlock>
  18.  
  19. <Button Click="Button_Click">Muuta dataa</Button>
  20.  
  21. <TextBox Text="{x:Bind CurrentTextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="20"></TextBox>
  22. <TextBlock Text="{x:Bind CurrentTextValue, Mode=OneWay}" Margin="20"></TextBlock>
  23.  
  24. <ComboBox Margin="20" SelectedItem="{x:Bind ComboBoxNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
  25. <ComboBoxItem Tag="1">Yksi</ComboBoxItem>
  26. <ComboBoxItem Tag="2">Kaksi</ComboBoxItem>
  27. <ComboBoxItem Tag="3">Kolme</ComboBoxItem>
  28. <ComboBoxItem Tag="4">Neljä</ComboBoxItem>
  29.  
  30. </ComboBox>
  31.  
  32. <TextBlock Text="{x:Bind ComboToNumber(ComboBoxNumber.Tag), Mode=OneWay}" Margin="20" FontSize="30"></TextBlock>
  33.  
  34. </StackPanel>
  35. </Page>
  36.  
  37.  
  38. <!-- NuGetilla ladattu lisäkomponentti, RadialGauge -esimerkki-->
  39.  
  40. <Page
  41. x:Class="UWPCourseApp2022.Views.Exercise1"
  42. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  43. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  44. xmlns:local="using:UWPCourseApp2022.Views"
  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. xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
  49. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  50.  
  51. <StackPanel>
  52. <TextBlock Margin="20" FontSize="60">Exercise 1</TextBlock>
  53. <Button Click="Button_Click">Parametrin kanssa -> Exercise 2</Button>
  54.  
  55. <Button Click="Button_Click_1">Arvo uusi nopeus</Button>
  56.  
  57. <controls:RadialGauge Width="500" x:Name="RadialGaugeControl" Value="70" Minimum="0"
  58. Maximum="180" TickSpacing="20" ScaleWidth="26" Unit="Units" TickBrush="Gainsboro"
  59. ScaleTickBrush="{ThemeResource ApplicationPageBackgroundThemeBrush}"
  60. NeedleWidth="5" TickLength="18">
  61. </controls:RadialGauge>
  62. </StackPanel>
  63. </Page>
  64.  
  65.  
  66.  
  67.  
  68.  
  69. C#:
  70. public sealed partial class Exercise3 : Page, INotifyPropertyChanged
  71. {
  72. public Exercise3()
  73. {
  74. PersonalText = "Data Binding sanoo että terve!";
  75. this.InitializeComponent();
  76. }
  77.  
  78. public String PersonalText { get; set; }
  79.  
  80. // koska tätä Propertya käytetään TwoWayBindingissa
  81. // on parempi laittaa "OnPropertyChanged"
  82. // set-metodin sisälle Propertyssä
  83. private String currentTextValue = "";
  84.  
  85. public String CurrentTextValue
  86. {
  87. get
  88. {
  89. return currentTextValue;
  90. }
  91.  
  92. set
  93. {
  94. currentTextValue = value;
  95. OnPropertyChanged("CurrentTextValue");
  96. }
  97. }
  98.  
  99. // HUOM: MainPage tässä tapauksessa toteuttaa rajapinnan INotifyPropertyChanged
  100. // seuraavat rivit ovat sitä varten olemassa, että tehdyt muutokset ServiceNameText-
  101. // propertyyn voidaan päivittää XAMLiin
  102. public event PropertyChangedEventHandler PropertyChanged;
  103.  
  104. private void OnPropertyChanged(string propertyName)
  105. {
  106. var eventHandler = this.PropertyChanged;
  107. if (eventHandler != null)
  108. eventHandler(this, new PropertyChangedEventArgs(propertyName));
  109. }
  110. }
  111.  
  112.  
  113.  
  114.  
  115. // vitsin lataaminen internetistä ja asettaminen Propertyyn
  116.  
  117. private void Button_Click(object sender, RoutedEventArgs e)
  118. {
  119. // Random rnd = new Random();
  120. // int randomNumber = rnd.Next(100);
  121. // PersonalText = "Muutettu! " + randomNumber;
  122.  
  123. // haetaan uusi vitsi internetistä
  124. String url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&format=txt&type=single";
  125.  
  126. // contents muuttujan sisältö voi tulla tiedostostakin,
  127. // ks. tiedoston lukeminen, esimerkit Moodlessa
  128. string contents;
  129. using (var wc = new System.Net.WebClient())
  130. contents = wc.DownloadString(url);
  131.  
  132. // asetetaan ladattu vitsi Propertyyn, joka näyttää Data Bindingin avulla
  133. // sen XAMLissa!
  134. PersonalText = contents;
  135. OnPropertyChanged("PersonalText");
  136. }
  137.  
  138. // JSON -versio samasta rajapinnasta
  139.  
  140. private void Button_Click(object sender, RoutedEventArgs e)
  141. {
  142. // Random rnd = new Random();
  143. // int randomNumber = rnd.Next(100);
  144. // PersonalText = "Muutettu! " + randomNumber;
  145.  
  146. // haetaan uusi vitsi internetistä
  147. //String url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&format=txt&type=single";
  148.  
  149. // haetaan JSON -muodossa data
  150. String url = "https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single";
  151.  
  152. // contents muuttujan sisältö voi tulla tiedostostakin,
  153. // ks. tiedoston lukeminen, esimerkit Moodlessa
  154. string contents;
  155. using (var wc = new System.Net.WebClient())
  156. contents = wc.DownloadString(url);
  157.  
  158. // muutetaan raaka JSON JsonObjectiksi
  159. JsonObject obj = JsonObject.Parse(contents);
  160.  
  161. // haetaan JsonObjectista kenttä nimeltä "joke"
  162. String joke = obj.GetNamedString("joke");
  163.  
  164. // asetetaan ladattu vitsi Propertyyn, joka näyttää Data Bindingin avulla
  165. // sen XAMLissa!
  166. PersonalText = joke;
  167. OnPropertyChanged("PersonalText");
  168. }
  169.  
  170.  
  171. // function binding esimerkki
  172.  
  173. private ComboBoxItem comboBoxNumber;
  174.  
  175. public ComboBoxItem ComboBoxNumber
  176. {
  177. get
  178. {
  179. return comboBoxNumber;
  180. }
  181.  
  182. set
  183. {
  184. comboBoxNumber = value;
  185. OnPropertyChanged("ComboBoxNumber");
  186. }
  187. }
  188.  
  189. public String ComboToNumber(object orig)
  190. {
  191. String number = orig as String;
  192. return number;
  193. }
  194.  
  195.  
  196. // RadialGaugen muutoskoodi:
  197.  
  198. private void Button_Click_1(object sender, RoutedEventArgs e)
  199. {
  200. Random rnd = new Random();
  201. int randomNumber = rnd.Next(180);
  202.  
  203.  
  204. RadialGaugeControl.Value = randomNumber;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement