Silverlight/XAML – Learning by Coding

[ sl2_json_dataimport.xaml --> Grafik anzeigen ]

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <!-- coded by Thomas Meinike 08/08 -->
  3: <UserControl x:Class="sl2_json_dataimport.Page"
  4:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  5:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  6:   xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
  7:   xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
  8:   Width="800" Height="600">
  9: 
 10:   <UserControl.Resources>
 11:     <Style x:Key="HeaderStyle" TargetType="dataprimitives:DataGridColumnHeader">
 12:       <Setter Property="Template">
 13:         <Setter.Value>
 14:           <ControlTemplate TargetType="dataprimitives:DataGridColumnHeader">
 15:             <Grid Grid.Column="6" Width="120" Height="25"
 16:               HorizontalAlignment="Center" ShowGridLines="True" Background="#F00">
 17:               <TextBlock Text="{TemplateBinding Content}"
 18:                 Foreground="#FFF" FontSize="16"
 19:                 HorizontalAlignment="Center" VerticalAlignment="Center"/>
 20:             </Grid>
 21:           </ControlTemplate>
 22:         </Setter.Value>
 23:       </Setter>
 24:     </Style>
 25: 
 26:     <Style TargetType="TextBlock" x:Key="CellStyle">
 27:       <Setter Property="VerticalAlignment" Value="Center"/>
 28:       <Setter Property="TextAlignment" Value="Center"/>
 29:       <!-- <Setter Property="FontSize" Value="14"/> -->
 30:     </Style>
 31:   </UserControl.Resources>
 32: 
 33:   <Canvas Loaded="HoleDaten">
 34: 
 35:     <TextBlock Canvas.Left="30" Canvas.Top="30" FontSize="18" Foreground="#00C"
 36:       Text="Olympia 2008-Medaillenspiegel mittels JSON-Daten importiert" x:Name="info"/>
 37: 
 38:     <data:DataGrid Canvas.Left="30" Canvas.Top="70" x:Name="dgrid"
 39:       AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeColumns="False" Padding="5"
 40:       HorizontalContentAlignment="Center" RowHeight="25" RowBackground="#FFF"
 41:       GridLinesVisibility="All" HeadersVisibility="Column" ColumnHeaderStyle="{StaticResource HeaderStyle}">
 42: 
 43:       <data:DataGrid.Columns>
 44: 
 45:         <data:DataGridTextColumn Header="Platz" Binding="{Binding P}"
 46:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 47:         <data:DataGridTextColumn Header="Land" Binding="{Binding L}"
 48:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 49:         <data:DataGridTextColumn Header="Gold" Binding="{Binding G}"
 50:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 51:         <data:DataGridTextColumn Header="Silber" Binding="{Binding S}"
 52:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 53:         <data:DataGridTextColumn Header="Bronze" Binding="{Binding B}"
 54:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 55:         <data:DataGridTextColumn Header="Anzahl" Binding="{Binding A}"
 56:           FontSize="14" ElementStyle="{StaticResource CellStyle}"/>
 57: 
 58:       </data:DataGrid.Columns>
 59: 
 60:     </data:DataGrid>
 61: 
 62:     <TextBlock Canvas.Left="30" Canvas.Top="500" x:Name="ausgabe" Text=""/>
 63: 
 64:   </Canvas>
 65: 
 66: 
 67:   <!-- // zusätzlich verwendeter VB.NET-Code in Page.xaml.vb:
 68: 
 69:   Imports System.Json
 70:   Imports System.Net
 71: 
 72:   Partial Public Class Page
 73:     Inherits UserControl
 74: 
 75:     Public Class MedaillenDaten
 76:       Private _P As Integer 'Platz
 77:       Private _L As String  'Land
 78:       Private _G As Integer 'Gold
 79:       Private _S As Integer 'Silber
 80:       Private _B As Integer 'Bronze
 81:       Private _A As Integer 'Anzahl
 82: 
 83:       Property P() As Integer
 84:         Get
 85:           Return _P
 86:         End Get
 87:         Set(ByVal value As Integer)
 88:           _P value
 89:         End Set
 90:       End Property
 91: 
 92:       Property L() As String
 93:         Get
 94:           Return _L
 95:         End Get
 96:         Set(ByVal value As String)
 97:           _L value
 98:         End Set
 99:       End Property
100: 
101:       Property G() As Integer
102:         Get
103:           Return _G
104:         End Get
105:         Set(ByVal value As Integer)
106:           _G value
107:         End Set
108:       End Property
109: 
110:       Property S() As Integer
111:         Get
112:           Return _S
113:         End Get
114:         Set(ByVal value As Integer)
115:           _S value
116:         End Set
117:       End Property
118: 
119:       Property B() As Integer
120:         Get
121:           Return _B
122:         End Get
123:         Set(ByVal value As Integer)
124:           _B value
125:         End Set
126:       End Property
127: 
128:       Property A() As Integer
129:         Get
130:           Return _A
131:         End Get
132:         Set(ByVal value As Integer)
133:           _A value
134:         End Set
135:       End Property
136: 
137:     End Class
138: 
139:     Public Sub New()
140:       InitializeComponent()
141:     End Sub
142: 
143:     Private Sub HoleDaten(ByVal sender As ObjectByVal e As RoutedEventArgs)
144:       Dim wclient As New WebClient
145:       AddHandler wclient.DownloadStringCompletedAddressOf DownloadStringCompleted
146: 
147:       Dim address As Uri = New Uri("medaillen_script.php"UriKind.Relative)
148:       wclient.DownloadStringAsync(address)
149:     End Sub
150: 
151:     Private Sub DownloadStringCompleted(ByVal sender As Object_
152:       ByVal e As DownloadStringCompletedEventArgs)
153: 
154:       'Parsen als JsonObject, falls jsonstring = "{...}"
155:       'Dim jsonobject As Json.JsonObject = Json.JsonObject.Parse(jsonstring)
156:       'weitere Auswertung der Unterobjekte von jsonobject
157: 
158:       'Parsen als JsonArray, falls jsonstring = "[...]"
159:       'Dim jsonarray As Json.JsonArray = Json.JsonArray.Parse(jsonstring)
160:       'weitere Auswertung der Arrayelemente von jsonarray
161: 
162:       If e.Error Is Nothing Then
163:         Dim jsonstring As String e.Result
164:         Dim jsonobject As Json.JsonObject Json.JsonObject.Parse(jsonstring)
165:         Dim datum As String jsonobject("Datum")
166:         Dim platzierungen As Json.JsonArray jsonobject("Platzierungen")
167: 
168:         Dim Source As List(Of MedaillenDaten) = New List(Of MedaillenDaten)
169:         For Each platzierung In platzierungen
170:           Source.Add(New MedaillenDaten() With _
171:            {.platzierung("P"), _
172:             .platzierung("L"), _
173:             .platzierung("G"), _
174:             .platzierung("S"), _
175:             .platzierung("B"), _
176:             .platzierung("A")})
177:         Next
178: 
179:         dgrid.ItemsSource Source
180:         info.Text info.Text " (" datum ")"
181: 
182:       Else
183:         ausgabe.Text e.Error.Message
184:       End If
185:     End Sub
186: 
187:   End Class
188: 
189:   -->
190: 
191: 
192:   <!-- von medaillen_script.php erzeugter JSON-String mit den Daten:
193: 
194:   {'Datum' 'JJJJ-MM-TT''Platzierungen' : [
195:   {'P' 1'L' 'Land''G' : ?, 'S' : ?, 'B' : ?, 'A' : ?},
196:   {... Plätze 2 bis 14 ...},
197:   {'P' 15'L' 'Land''G' : ?, 'S' : ?, 'B' : ?, 'A' : ?}]}
198: 
199:   -->
200: 
201: </UserControl>

[zum Anfang]