Traduction▲
Ce tutoriel est la traduction la plus fidèle possible du tutoriel original de Peter Bull, Windows Phone 7 - 70's style Flip Clock using Silverlight on Windows Phone 7.
Introduction▲
Flip Clock émule le style classique « flip clock » de divers réveils et de la signalétique aéroportuaire pour une horloge en utilisant Sillverlight sur Windows Phone 7.
« Flip Clock » du style des années 70 en utilisant Silverlight sur Windows Phone 7▲
Étape 1▲
Démarrez Microsoft Visual Web Developer 2010 Express pour Windows Phone, ensuite sélectionnez Fichier puis Nouveau Projet… Sélectionnez Visual C# ensuite Silverlight for Windows Phone puis Application Windows Phone 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▲
Étape 3▲
Sélectionnez Projet depuis le menu principal puis Ajouter un nouvel élément…, ensuite sélectionnez le Contrôle utilisateur Windows Phone, puis modifiez le Nom en Flip.xaml :
Étape 4▲
Ajoutez le nouveau contrôle utilisateur au Projet en cliquant sur Ajouter, ensuite dans le volet XAML du contrôle utilisateur enlevez d:DesignHeight="480" d:DesignWidth="480" de la balise <UserControl ...>. Enlevez également Background="{StaticResource PhoneChromeBrush}" de <Grid x:Name="LayoutRoot" ...> :

Étape 5▲
Puisque nous en sommes au volet XAML du contrôle utilisateur Flip.xaml, entre les lignes <Grid x:Name="LayoutRoot" Background="#FF1F1F1F"> et </Grid>, tapez le code XAML suivant :
<Grid Height="140" Width="140">
<Grid.Resources>
<Style x:Key="Text" TargetType="TextBlock">
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
<Setter Property="FontSize" Value="90"/>
<Setter Property="Foreground" Value="{StaticResource PhoneBackgroundBrush}"/>
</Style>
<Style x:Key="Border" TargetType="Border">
<Setter Property="CornerRadius" Value="8"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneSubtleColor}"/>
<Setter Property="BorderThickness" Value="1,1,1,1"/>
</Style>
<LinearGradientBrush x:Key="BackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{StaticResource PhoneForegroundColor}" Offset="1"/>
<GradientStop Color="{StaticResource PhoneAccentColor}"/>
</LinearGradientBrush>
<Storyboard x:Name="FlipAnimation">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="BlockFlip"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<EasingDoubleKeyFrame Value="1" KeyTime="0">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseOut" Bounces="1" Bounciness="6"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame Value="-1" KeyTime="00:00:00.8">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseOut" Bounces="1" Bounciness="6"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBlockFlipTop"
Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="00:00:00.4">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBlockFlipBottom"
Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="00:00:00.4">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Border Style="{StaticResource Border}" Background="{StaticResource BackgroundBrush}" x:Name="BlockTop">
<TextBlock Style="{StaticResource Text}" HorizontalAlignment="Center" VerticalAlignment="Top" Text="01" x:Name="textBlockTop"/>
</Border>
<Border Style="{StaticResource Border}" Grid.Row="1" x:Name="BlockBottom">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{StaticResource PhoneForegroundColor}" />
<GradientStop Color="{StaticResource PhoneAccentColor}" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock Margin="0,0,0,16" Style="{StaticResource Text}" HorizontalAlignment="Center" VerticalAlignment="Bottom"
RenderTransformOrigin="0.5,0" Text="00" x:Name="textBlockBottom"/>
</Border>
<Border Style="{StaticResource Border}" Background="{StaticResource BackgroundBrush}" RenderTransformOrigin="0.5,1" x:Name="BlockFlip">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Grid>
<TextBlock Style="{StaticResource Text}" HorizontalAlignment="Center" VerticalAlignment="Top" Text="00"
x:Name="textBlockFlipTop"/>
<TextBlock Style="{StaticResource Text}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Visibility="Collapsed"
RenderTransformOrigin="0.5,0.5" x:Name="textBlockFlipBottom">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="70"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
</Border>
</Grid>XAML :
Design :

Étape 6▲
Faites un clic droit sur la page ou sur l'entrée pour Flip.xaml dans l'Explorateur de solutions et choisissez l'option Afficher le code. Dans la vue de code, au-dessus de public Flip() tapez les propriétés de dépendance, les propriétés etles méthodes suivantes :
public static readonly DependencyProperty TextPrevProperty =
DependencyProperty.Register("TextPrev", typeof(string),
typeof(Flip), null);
public static readonly DependencyProperty TextNextProperty =
DependencyProperty.Register("TextNext", typeof(string),
typeof(Flip), null);
public string TextPrev
{
get { return (string)GetValue(TextPrevProperty); }
set
{
SetValue(TextPrevProperty, value);
textBlockBottom.Text = TextPrev;
textBlockFlipTop.Text = TextPrev;
}
}
public string TextNext
{
get { return (string)GetValue(TextNextProperty); }
set
{
SetValue(TextNextProperty, value);
textBlockTop.Text = TextNext;
textBlockFlipBottom.Text = TextNext;
}
}
public void Value(string source, string target)
{
TextNext = target;
TextPrev = source;
FlipAnimation.Begin();
}Étape 7▲
Sélectionnez Build Solution depuis le menu Debug :

Étape 8▲
Quand la compilation est terminée, retournez au concepteur de vues MainPage en sélectionnant l'onglet MainPage.xaml. Dans le volet XAML, entre les lignes <Grid x:Name="ContentGrid" Grid.Row="1"> et </Grid>, tapez le code StackPanel XAML suivant :
<StackPanel Height="200" Width="420"
HorizontalAlignment="Center" Orientation="Horizontal">
</StackPanel>XAML :

Design :
Étape 9▲
Étape 10▲
Dessinez trois contrôles Flip sur le StackPanel en glissant les contrôles Flip de la Boite à outils sur le StackPanel, ensuite dans le volet XAML modifiez les lignes <my:Flip ...> comme ceci :
<my:Flip x:Name="Hours"/>
<my:Flip x:Name="Minutes"/>
<my:Flip x:Name="Seconds"/>
XAML :

Design :
Étape 11▲
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, au-dessus de namespace FlipClock, tapez ceci :
using System.Windows.Threading;Également dans la vue de code, au-dessus de public MainPage(), tapez les déclarations suivantes :
private DispatcherTimer _timer = new DispatcherTimer();
private int _seconds = DateTime.Now.Second;
private int _minutes = DateTime.Now.Minute;
private int _hours = DateTime.Now.Hour;Étape 12▲
Toujours dans la vue de code pour MainPage.xaml.cs, au-dessus de public MainPage(), tapez le gestionnaire d'événements suivant :
private void Timer_Tick(object sender, EventArgs e)
{
if (_seconds == 59)
{
Seconds.Value(_seconds.ToString("00"), 0.ToString("00"));
_seconds = 0;
if (_minutes == 59)
{
Minutes.Value(_minutes.ToString("00"), 0.ToString("00"));
_minutes = 0;
if (_hours == 23)
{
Hours.Value(_hours.ToString("00"), 0.ToString("00"));
_hours = 0;
}
else
{
Hours.Value(_hours.ToString("00"), (_hours + 1).ToString("00"));
_hours += 1;
}
}
else
{
Minutes.Value(_minutes.ToString("00"), (_minutes + 1).ToString("00"));
_minutes += 1;
}
}
else
{
Seconds.Value(_seconds.ToString("00"), (_seconds + 1).ToString("00"));
_seconds += 1;
}
}Étape 13▲
Puisque nous en sommes à la vue de code, dans le constructeur public MainPage() en dessous de InitializeComponent();, tapez les lignes de code suivantes :
PageTitle.Text = "flip clock";
Seconds.TextPrev = _seconds.ToString("00");
Minutes.TextPrev = _minutes.ToString("00");
Hours.TextPrev = _hours.ToString("00");
_timer.Tick += Timer_Tick;
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Start();
Étape 14▲
Enregistrez le Projet maintenant que vous avez terminé l'application Windows Phone Silverlight. Sélectionnez l'option Windows Phone Emulator ensuite sélectionnez Debug puis Démarrer le débogage ou cliquez sur Démarrer le débogage :

Dès que vous l'aurez fait, ce qui suit apparaîtra dans l'émulateur Windows Phone une fois chargé :
Étape 15▲
Vous pouvez ensuite Arrêter l'application en sélectionnant la fenêtre d'application Visual Studio 2010 et en cliquant sur le bouton Arrêter le débogage :

Conclusion▲
Ceci est un « flip clock » simple utilisant un contrôle utilisateur Flip. Il serait possible d'utiliser le contrôle utilisateur Flip pour afficher du texte comme dans les signalétiques des aéroports mentionnées précédemment, par exemple. Vous pouvez modifier le contrôle pour ressembler davantage à un « flip clock » classique ou autre chose - personnalisez-le !
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 ClaudeLELOUP pour sa relecture orthographique et ses propositions.


















