Silverlight/XAML – Learning by Coding

[ sl2_zip_handling.xaml --> Grafik anzeigen ]

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <!-- coded by Thomas Meinike 12/08 -->
  3: <UserControl x:Class="sl2_zip_handling.Page"
  4:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  5:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  6:   Width="800" Height="600" Loaded="LoadZIP">
  7: 
  8:   <Canvas x:Name="fotos">
  9: 
 10:     <TextBlock Canvas.Left="330" Canvas.Top="15" Foreground="#999"
 11:       FontFamily="Lucida" FontSize="18" Text="Bilder dynamisch aus einem ZIP-Archiv laden"/>
 12: 
 13:     <!-- Fortschrittsbalken -->
 14:     <Rectangle Canvas.Left="20" Canvas.Top="560" Width="0" Height="25"
 15:       Stroke="#EEE" StrokeThickness="0.5" RadiusX="12.5" RadiusY="12.5" x:Name="pr_rect">
 16:       <Rectangle.Fill>
 17:         <LinearGradientBrush>
 18:           <GradientStop Color="#DFF" Offset="0.0"/>
 19:           <GradientStop Color="#7AC" Offset="0.8"/>
 20:           <GradientStop Color="#589" Offset="1.0"/>
 21:         </LinearGradientBrush>
 22:       </Rectangle.Fill>
 23:     </Rectangle>
 24: 
 25:     <!-- Fortschrittswert in Prozent -->
 26:     <TextBlock Canvas.Left="220" Canvas.Top="564" Width="430" Height="25" Foreground="#00C"
 27:       FontFamily="Lucida" FontSize="12" Text="%" x:Name="pr_proz"/>
 28:     
 29:   </Canvas>
 30: 
 31: 
 32:   <!-- // zusätzlich verwendeter VB.NET-Code in Page.xaml.vb:
 33: 
 34:   Imports System.IO
 35:   Imports System.Net
 36:   Imports System.Xml.Linq
 37:   Imports System.Windows.Resources
 38:   Imports System.Windows.Media.Imaging
 39: 
 40:   Partial Public Class Page
 41:     Inherits UserControl
 42: 
 43:     Public Sub New()
 44:       InitializeComponent()
 45:     End Sub
 46: 
 47:     Private Sub LoadZIP(ByVal sender As System.ObjectByVal e As System.Windows.RoutedEventArgs)
 48:       Dim wclient As New WebClient
 49:       Dim address As Uri = New Uri("fotos.zip"UriKind.Relative)
 50:       AddHandler wclient.OpenReadCompletedAddressOf OpenReadZIP
 51:       AddHandler wclient.DownloadProgressChangedAddressOf DownloadProgress
 52:       wclient.OpenReadAsync(address)
 53:     End Sub
 54: 
 55:     Private Sub DownloadProgress(ByVal sender As ObjectByVal e As DownloadProgressChangedEventArgs)
 56:       Dim pr As Double Math.Floor(e.ProgressPercentage)
 57:       pr_rect.Width pr 4.3 'maximale Rechteck-Breite: 430
 58:       pr_proz.Text pr.ToString() + "%"
 59:     End Sub
 60: 
 61:     Private Sub OpenReadZIP(ByVal sender As ObjectByVal e As OpenReadCompletedEventArgs)
 62:       If e.Error Is Nothing Then
 63:         Try
 64:           'ZIP-Inhalt verarbeiten (files.xml und foto_01.png bis foto_04.png)
 65:           Dim zip_stream As Stream e.Result
 66:           Dim stream_source As StreamResourceInfo = New StreamResourceInfo(zip_streamNothing)
 67:           Dim xmlSourceInfo As StreamResourceInfo _
 68:             Application.GetResourceStream(stream_source, New Uri("files.xml"UriKind.Relative))
 69: 
 70:           'Bildnamen aus XML-File lesen und Bildressourcen zuweisen
 71:           Dim xmldoc XElement.Load(xmlSourceInfo.Stream)
 72:           Dim i As Integer 0
 73: 
 74:           For Each file In xmldoc.Descendants("file")
 75:             Dim img_filename As String file.FirstNode.ToString() 'Bildname aus XML-File
 76:             'Bildinhalte verarbeiten
 77:             Dim image_source As StreamResourceInfo _
 78:               Application.GetResourceStream(stream_source, New Uri(img_filenameUriKind.Relative))
 79:             Dim bitmap As New BitmapImage
 80:             bitmap.SetSource(image_source.Stream)
 81: 
 82:             Dim img_brush As ImageBrush = New ImageBrush
 83:             img_brush.ImageSource bitmap
 84: 
 85:             'Rechteck mit abgerundeten Ecken und Bild als Füllung erzeugen
 86:             Dim xaml_rect As String _
 87:              "<Rectangle xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " _
 88:              "Canvas.Left='" + (20 150).ToString() + "' Canvas.Top='" + (20 125).ToString() + "' " _
 89:              "Width='250' Height='187' RadiusX='15' RadiusY='15'/>"
 90: 
 91:             Dim rectangle DirectCast(System.Windows.Markup.XamlReader.Load(xaml_rect), Rectangle)
 92:             rectangle.Fill img_brush
 93: 
 94:             fotos.Children.Add(rectangle)
 95:             i 1
 96:           Next
 97: 
 98:         Catch ex As Exception
 99:           pr_proz.Foreground = New SolidColorBrush(Colors.Red)
100:           pr_proz.Text "Fehler beim Laden!"
101:         End Try
102:       End If
103:     End Sub
104: 
105:   End Class
106: 
107:   -->
108: 
109: 
110:   <!-- // zusätzlich verwendeter XML-Code in files.xml als Teil von fotos.zip:
111: 
112:   <?xml version="1.0" encoding="UTF-8"?>
113:   <files info="Bilddateien in fotos.zip">
114:     <file>foto_01.png</file>
115:     <file>foto_02.png</file>
116:     <file>foto_03.png</file>
117:     <file>foto_04.png</file>
118:   </files>
119: 
120:   -->
121: 
122: </UserControl>

[zum Anfang]