Traduction▲
Ce tutoriel est la traduction la plus fidèle possible du tutoriel original de Peter Bull, Microsoft Silverlight 4 - Silverlight Analogue Clock with Alarm.
Introduction▲
Alarm Clock fait usage d'un contrôle utilisateur Silverlight pour réaliser une horloge analogique qui affiche l'heure et les paramètres d'alarme afin de créer un réveil simple.
Horloge analogique avec alarme en Silverlight▲
Étape 1▲
Démarrez Microsoft Visual Web Developer 2010 Express, ensuite sélectionnez Fichier puis Nouveau Projet... Sélectionnez Visual Basic ensuite Application Silverlight dans les modèles installés, sélectionnez un emplacement si vous le souhaitez, puis entrez un nom pour le projet et appuyez sur OK :
Étape 2▲
Une nouvelle fenêtre Nouvelle application Silverlight devrait apparaître, décochez la case Héberger l'application Silverlight sur un nouveau site Web, puis sélectionnez la Version de Silverlight ciblée :
Étape 3▲
Une page vierge nommée MainPage.xaml devrait alors apparaître :

Étape 4▲
Sélectionnez Projet ensuite Ajouter un nouvel élément..., et sélectionnez le modèle Contrôle utilisateur Silverlight s'il ne l'est pas déjà, ensuite changez le Nom à Clock.xaml :
Étape 5▲
Ajoutez le nouveau contrôle utilisateur au Projet en cliquant sur Ajouter, ensuite dans le volet XAML pour le contrôle utilisateur Clock.xaml, pour la balise <UserControl> modifiez les attributs DesignHeight et DesignWidth à 250 :

Étape 6▲
Retournez dans le concepteur de vues pour Clock en sélectionnant l'onglet Clock.xaml.
Puis dans la section Tous les contrôles Silverlight de la Boîte à outils, sélectionnez le contrôle Canvas :
Étape 7▲
Dessinez un Canvas sur la page, ensuite dans le volet XAML, au-dessus de </Grid> modifiez la ligne Canvas1 comme ceci :
<Canvas Height="250" Width="250" Name="Display"/>Voir ci-dessous :

Étape 8▲
Faites un clic droit sur la page ou sur l'entrée pour Clock.xaml dans l'Explorateur de solutions et choisissez l'option Afficher le code. Dans la vue de code, en dessous de la ligne Inherits UserControl tapez les déclarations suivantes :
Private ReadOnly COLOR_TOMATO As Color = Color.FromArgb(255, 255, 99, 71)
Private ReadOnly COLOR_GAINSBORO As Color = Color.FromArgb(255, 220, 220, 220)
Private ReadOnly COLOR_WHITESMOKE As Color = Color.FromArgb(255, 245, 245, 245)
Private ReadOnly COLOR_ROYALBLUE As Color = Color.FromArgb(255, 65, 105, 225)
Private WithEvents _timer As New Windows.Threading.DispatcherTimer
Private _time As DateTime = Now
Private _alarm As DateTime
Private _markers As New Canvas
Private _face As New Canvas
Private _secondsHand As Rectangle
Private _minutesHand As Rectangle
Private _hoursHand As Rectangle
Private _alarmMinutesHand As Rectangle
Private _alarmHoursHand As Rectangle
Private _diameter As Double
Private _secondsWidth As Integer = 1
Private _secondsHeight As Integer
Private _minutesWidth As Integer = 5
Private _minutesHeight As Integer
Private _hoursWidth As Integer = 8
Private _hoursHeight As Integer
Private _alarmMinutesWidth As Integer = 5
Private _alarmMinutesHeight As Integer
Private _alarmHoursWidth As Integer = 8
Private _alarmHoursHeight As Integer
Private _isEnabled As Boolean = True
Private _isRealTime As Boolean = True
Private _showSeconds As Boolean = True
Private _showMinutes As Boolean = True
Private _showHours As Boolean = True
Private _showAlarm As Boolean = True
Private _hasAlarm As Boolean = False
Private _faceColour As Color = COLOR_ROYALBLUE
Private _rimColour As Color = Colors.Blue
Private _markerColour As Color = Colors.White
Private _secondColour As Color = COLOR_TOMATO
Private _minuteColour As Color = COLOR_GAINSBORO
Private _hourColour As Color = COLOR_WHITESMOKE
Private _alarmColour As Color = Colors.Orange
Public Event Triggered()
Étape 9▲
Pendant que nous en sommes à la vue de code pour le contrôle utilisateur Clock, au-dessus du Public Sub New() de Partial Public Class Clock, tapez les fonctions suivantes :
Private Function Hand(ByRef Width As Double, ByRef Height As Double, _
ByRef Background As Brush, ByRef Foreground As Brush, _
ByRef RadiusX As Double, ByRef RadiusY As Double, _
ByRef Thickness As Double)
Dim _hand As New Rectangle
_hand.Width = Width
_hand.Height = Height
_hand.Fill = Background
_hand.StrokeThickness = Thickness
_hand.RadiusX = RadiusX
_hand.RadiusY = RadiusY
_hand.Stroke = Foreground
Return _hand
End Function
Private Function TransformGroup(ByRef Angle As Double, _
ByRef X As Double, ByRef Y As Double) As TransformGroup
Dim _transformGroup As New TransformGroup
Dim _firstTranslate As New TranslateTransform
_firstTranslate.X = X
_firstTranslate.Y = Y
_transformGroup.Children.Add(_firstTranslate)
Dim _rotateTransform As New RotateTransform
_rotateTransform.Angle = Angle
_transformGroup.Children.Add(_rotateTransform)
Dim _secondTranslate As New TranslateTransform
_secondTranslate.X = _diameter / 2
_secondTranslate.Y = _diameter / 2
_transformGroup.Children.Add(_secondTranslate)
Return _transformGroup
End FunctionÉtape 10▲
Toujours dans le vue de code pour le contrôle utilisateur Clock, au-dessus du Public Sub New() de Partial Public Class Clock et en dessous des fonctions, tapez les Subs suivants :
Private Sub SecondHand(ByVal Seconds As Integer)
If _secondsHand IsNot Nothing AndAlso _face.Children.Contains(_secondsHand) Then
_face.Children.Remove(_secondsHand)
End If
If _showSeconds Then
_secondsHand = Hand(_secondsWidth, _secondsHeight,
New SolidColorBrush(_secondColour), Nothing, 0, 0, 0)
_secondsHand.RenderTransform = TransformGroup(Seconds * 6,
-_secondsWidth / 2, -_secondsHeight + 4.25)
If Not _face.Children.Contains(_secondsHand) Then
_face.Children.Add(_secondsHand)
End If
End If
End Sub
Private Sub MinuteHand(ByVal Minutes As Integer, ByVal Seconds As Integer)
If _minutesHand IsNot Nothing AndAlso _face.Children.Contains(_minutesHand) Then
_face.Children.Remove(_minutesHand)
End If
If _showMinutes Then
_minutesHand = Hand(_minutesWidth, _minutesHeight,
New SolidColorBrush(_minuteColour), Nothing, 2, 2, 0.6)
_minutesHand.RenderTransform = TransformGroup(6 * Minutes + Seconds / 10,
-_minutesWidth / 2, -_minutesHeight + 4.25)
End If
If _face.Children.Contains(_minutesHand) = False Then
_face.Children.Add(_minutesHand)
End If
End Sub
Private Sub HourHand(ByVal Hours As Integer, ByVal Minutes As Integer, ByVal Seconds As Integer)
If _hoursHand IsNot Nothing AndAlso _face.Children.Contains(_hoursHand) Then
_face.Children.Remove(_hoursHand)
End If
If _showHours Then
_hoursHand = Hand(_hoursWidth, _hoursHeight,
New SolidColorBrush(_hourColour), Nothing, 3, 3, 0.6)
_hoursHand.RenderTransform = TransformGroup(30 * Hours + Minutes / 2 + Seconds / 120,
-_hoursWidth / 2, -_hoursHeight + 4.25)
End If
If _face.Children.Contains(_hoursHand) = False Then
_face.Children.Add(_hoursHand)
End If
End Sub
Private Sub AlarmMinuteHand(ByVal Minutes As Integer, ByVal Seconds As Integer)
If _alarmMinutesHand IsNot Nothing AndAlso _face.Children.Contains(_alarmMinutesHand) Then
_face.Children.Remove(_alarmMinutesHand)
End If
If _hasAlarm Then
If _showAlarm Then
_alarmMinutesHand = Hand(_alarmMinutesWidth, _alarmMinutesHeight,
New SolidColorBrush(_alarmColour), Nothing, 2, 2, 0.6)
_alarmMinutesHand.RenderTransform = TransformGroup(6 * Minutes + Seconds / 10,
-_alarmMinutesWidth / 2, -_alarmMinutesHeight + 4.25)
End If
If _face.Children.Contains(_alarmMinutesHand) = False Then
_face.Children.Add(_alarmMinutesHand)
End If
End If
End Sub
Private Sub AlarmHourHand(ByVal Hours As Integer, ByVal Minutes As Integer, ByVal Seconds As Integer)
If _alarmHoursHand IsNot Nothing AndAlso _face.Children.Contains(_alarmHoursHand) Then
_face.Children.Remove(_alarmHoursHand)
End If
If _hasAlarm Then
If _showAlarm Then
_alarmHoursHand = Hand(_alarmHoursWidth, _alarmHoursHeight,
New SolidColorBrush(_alarmColour), Nothing, 3, 3, 0.6)
_alarmHoursHand.RenderTransform = TransformGroup(30 * Hours + Minutes / 2 + Seconds / 120,
-_alarmHoursWidth / 2, -_alarmHoursHeight + 4.25)
End If
If _face.Children.Contains(_alarmHoursHand) = False Then
_face.Children.Add(_alarmHoursHand)
End If
End If
End Sub
Private Sub Layout(ByRef Canvas As Canvas)
Dim _rim As New Ellipse
Dim _inner As New Ellipse
Canvas.Children.Clear()
_diameter = Canvas.Width
_rim.Height = _diameter
_rim.Width = _diameter
_rim.Fill = New SolidColorBrush(_rimColour)
Canvas.Children.Add(_rim)
_inner.Width = _diameter - 30
_inner.Height = _diameter - 30
_inner.Fill = New SolidColorBrush(_faceColour)
Canvas.SetTop(_inner, 15)
Canvas.SetLeft(_inner, 15)
Canvas.Children.Add(_inner)
_markers.Children.Clear()
_markers.Width = _diameter
_markers.Height = _diameter
For i As Integer = 0 To 59
Dim _marker As New Rectangle
If i Mod 5 = 0 Then
_marker.Width = 3
_marker.Height = 8
_marker.Fill = New SolidColorBrush(_markerColour)
_marker.RenderTransform = TransformGroup(i * 6, -(_marker.Width / 2),
-(_marker.Height * 2 + 4.5 - _rim.StrokeThickness / 2 - _diameter / 2 - 8))
Else
_marker.Width = 1
_marker.Height = 4
_marker.Fill = New SolidColorBrush(_markerColour)
_marker.RenderTransform = TransformGroup(i * 6, -(_marker.Width / 2),
-(_marker.Height * 2 + 12.75 - _rim.StrokeThickness / 2 - _diameter / 2 - 8))
End If
_markers.Children.Add(_marker)
Next
Canvas.Children.Add(_markers)
_face.Width = _diameter
_face.Height = _diameter
Canvas.Children.Add(_face)
_secondsHeight = _diameter / 2 - 20
_minutesHeight = _diameter / 2 - 40
_hoursHeight = _diameter / 2 - 60
_alarmMinutesHeight = _diameter / 2 - 25
_alarmHoursHeight = _diameter / 2 - 50
End Sub
Private Sub Timer_Tick(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles _timer.Tick
If _isRealTime Then _time = Now()
If _hasAlarm Then
If Format(_time, "hh:mm:ss") = _
Format(_alarm, "hh:mm") & ":00" Then
RaiseEvent Triggered()
End If
End If
AlarmMinuteHand(_alarm.Minute, _alarm.Second)
AlarmHourHand(_alarm.Hour, _alarm.Minute, _alarm.Second)
SecondHand(_time.Second)
MinuteHand(_time.Minute, _time.Second)
HourHand(_time.Hour, _time.Minute, _time.Second)
End Sub
Étape 11▲
Puisque nous en sommes toujours à la vue de code pour le contrôle utilisateur Clock, dans le constructeur Public Sub New() en dessous de la ligne InitializeComponent(), tapez le code suivant :
Layout(Display)
_timer.Start()
Étape 12▲
En restant toujours dans le vue de code pour le contrôle utilisateur Clock, au-dessus du End Class de Partial Public Class Clock, tapez les propriétés suivantes :
Public Property FaceColour() As Color
Get
Return _faceColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_faceColour = COLOR_ROYALBLUE
Else
_faceColour = Value
End If
Layout(Display)
End Set
End Property
Public Property RimColour() As Color
Get
Return _rimColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_rimColour = Colors.Blue
Else
_rimColour = Value
End If
Layout(Display)
End Set
End Property
Public Property MarkerColour() As Color
Get
Return _markerColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_markerColour = Colors.White
Else
_markerColour = Value
End If
Layout(Display)
End Set
End Property
Public Property SecondHandColor() As Color
Get
Return _secondColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_secondColour = COLOR_TOMATO
Else
_secondColour = Value
End If
End Set
End Property
Public Property MinuteHandColor() As Color
Get
Return _minuteColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_minuteColour = COLOR_GAINSBORO
Else
_minuteColour = Value
End If
End Set
End Property
Public Property HourHandColor() As Color
Get
Return _hourColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_hourColour = COLOR_WHITESMOKE
Else
_hourColour = Value
End If
End Set
End Property
Public Property AlarmHandsColor() As Color
Get
Return _alarmColour
End Get
Set(ByVal Value As Color)
If Value = Nothing Then
_alarmColour = Colors.Orange
Else
_alarmColour = Value
End If
End Set
End Property
Public Property Enabled As Boolean
Get
Return _isEnabled
End Get
Set(ByVal value As Boolean)
_isEnabled = value
If _isEnabled Then
_timer.Start()
Else
_timer.Stop()
End If
End Set
End Property
Public Property RealTime As Boolean
Get
Return _isRealTime
End Get
Set(ByVal value As Boolean)
_isRealTime = value
End Set
End Property
Public Property ShowSeconds As Boolean
Get
Return _showSeconds
End Get
Set(ByVal value As Boolean)
_showSeconds = value
End Set
End Property
Public Property ShowMinutes As Boolean
Get
Return _showMinutes
End Get
Set(ByVal value As Boolean)
_showMinutes = value
End Set
End Property
Public Property ShowHours As Boolean
Get
Return _showHours
End Get
Set(ByVal value As Boolean)
_showHours = value
End Set
End Property
Public Property ShowAlarm As Boolean
Get
Return _showAlarm
End Get
Set(ByVal value As Boolean)
_showAlarm = value
End Set
End Property
Public Property Time() As DateTime
Get
Return _time
End Get
Set(ByVal Value As DateTime)
_time = Value
End Set
End Property
Public Property Alarm() As DateTime
Get
Return _alarm
End Get
Set(ByVal Value As DateTime)
_alarm = Value
_hasAlarm = Not _alarm = Nothing
End Set
End PropertyÉtape 13▲
Retournez dans le concepteur de vues pour MainPage en sélectionnant l'onglet MainPage.xaml.
Puis dans la section Tous les contrôles Silverlight de la Boîte à outils, sélectionnez le contrôle Canvas :
Étape 14▲
Dessinez deux Canvas sur la page, ensuite dans le volet XAML au-dessus de </Grid> modifiez les lignes <Canvas> comme ceci :
<Canvas Height="35" Width="400" VerticalAlignment="Top" HorizontalAlignment="Left" Name="Toolbar"></Canvas>
<Canvas Height="265" Width="400" Margin="0,35,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Name="Content"></Canvas>Voir ci-dessous :

Étape 15▲
Étape 16▲
Dessinez deux ComboBox sur la barre d'outils du Canvas en glissant les ComboBox de la Boîte à outils sur le Canvas, ensuite dans le volet XAML entre les balises <Canvas> et </Canvas> modifiez les lignes <ComboBox> comme ceci :
<ComboBox Canvas.Left="6" Canvas.Top="6" Height="23" Width="75" Name="AlarmHours"/>
<ComboBox Canvas.Left="87" Canvas.Top="6" Height="23" Width="75" Name="AlarmMinutes"/>Voir ci-dessous :

Étape 17▲
Étape 18▲
Dessinez un CheckBox sur le Canvas supérieur (barre d'outils) à côté des boutons, et modifiez la ligne <CheckBox> comme ceci :
<CheckBox Canvas.Left="168" Canvas.Top="10" Height="17" Width="75" Content="Set Alarm" Name="SetAlarm"/>Voir ci-dessous :

Étape 19▲
Sélectionnez Debug ensuite l'option Build AlarmClock du menu :

Étape 20▲
Puis dans la section Contrôles AlarmClock de la Boîte à outils, sélectionnez le contrôle Clock (AlarmClock) :
Étape 21▲
Dessinez un Clock sur le plus grand Canvas (ou Canvas Content) et dans le volet XAML modifiez la ligne <my:Clock> comme ceci :
<my:Clock Canvas.Left="75" Canvas.Top="10" x:Name="AlarmClock"/>Voir ci-dessous :

Étape 22▲
Faites un clic droit sur la page ou sur l'entrée pour MainPage.xaml dans l'Explorateur de solutions et choisissez l'option Afficher le code.
Dans la vue de code pour le constructeur Public Sub New() en dessous de la ligne InitializeComponent(), tapez le code suivant :
For H As Integer = 0 To 23
AlarmHours.Items.Add(Format(H, "00"))
Next
AlarmHours.SelectedItem = CStr(Format(Now.Hour, "00"))
For M As Integer = 0 To 59
AlarmMinutes.Items.Add(Format(M, "00"))
Next
AlarmMinutes.SelectedItem = CStr(Format(Now.Minute, "00"))
Étape 23▲
Toujours dans la vue de code, en dessous du End Sub pour le constructeur Public Sub New(), tapez le Sub gestionnaire d'événement suivant :
Private Sub AlarmClock_Triggered() Handles AlarmClock.Triggered
MessageBox.Show("Alarm!", "Alarm Clock", MessageBoxButton.OK)
End Sub
Étape 24▲
Retournez dans le concepteur de vues en sélectionnant l'onglet MainPage.xaml, ou faites un clic droit sur la page ou sur l'entrée pour MainPage.xaml dans l'Explorateur de solutions et choisissez l'option Concepteur de vues.
Cliquez sur le CheckBox « Set Alarm », ensuite allez dans la boîte Propriétés et cliquez sur l'onglet Événements :
Double-cliquez sur l'événement Click et tapez ceci dans le Sub SetAlarm_Click :
If SetAlarm.IsChecked Then
AlarmClock.Alarm = New DateTime(Now.Year, Now.Month, Now.Day, _
CInt(AlarmHours.SelectedItem), _
CInt(AlarmMinutes.SelectedItem), 0)
Else
AlarmClock.Alarm = Nothing
End If
Étape 25▲
Enregistrez le projet maintenant que vous avez terminé l'application Silverlight. Sélectionnez Debug ensuite Démarrer le débogage ou cliquez sur Démarrer le débogage :

Une fois que vous l'avez fait, ce qui suit apparaîtra dans une nouvelle fenêtre du navigateur :

Étape 26▲
Réglez l'alarme en utilisant les ComboBox pour sélectionner l'heure et les minutes, puis cochez la case « Set Alarm ». Un MessageBox apparaîtra quand l'alarme est déclenchée :

Étape 27▲
Fermez l'application et la fenêtre du navigateur en cliquant sur le bouton Fermer
en haut à droite de la fenêtre de l'application et du navigateur Web pour Arrêter l'application.
Conclusion▲
Ceci est une application de réveil simple. Vous pouvez modifier les couleurs utilisées sur l'horloge en la sélectionnant, puis en allant dans la boîte Propriétés et en modifiant les propriétés telles que l'aiguille des heures, la couleur du cadran, etc. Vous pourriez également personnaliser l'horloge de manière à lui faire utiliser une brosse à dégradé (GradientBrush) ou avec d'autres effets - personnalisez-la !
Liens▲
Remerciements▲
Je tiens ici à remercier Peter Bull de m'avoir autorisé à traduire son tutoriel.
Je remercie tomlev pour sa relecture technique et ses propositions.
Je remercie également FirePrawn pour sa relecture orthographique et ses propositions.























