Created
October 29, 2014 23:25
-
-
Save jamesmundy/2b19da71521fb9fd1c0c to your computer and use it in GitHub Desktop.
Windows 8 Tile Control Code-behind: https://medium.com/p/84454669b005
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Media; | |
namespace W8TileControl | |
{ | |
public sealed partial class TileControl : UserControl | |
{ | |
public static readonly DependencyProperty FrontImageProperty = DependencyProperty.Register( | |
"FrontImage", | |
typeof(ImageSource), | |
typeof(TileControl), | |
new PropertyMetadata(null, OnFrontImagePropertyChanged)); | |
public static readonly DependencyProperty RearImageProperty = DependencyProperty.Register( | |
"RearImage", | |
typeof(ImageSource), | |
typeof(TileControl), | |
new PropertyMetadata(null, OnRearImagePropertyChanged)); | |
private static readonly Random rand = new Random(); | |
private readonly DispatcherTimer timer = new DispatcherTimer(); | |
private bool flipped; | |
public TileControl() | |
{ | |
this.InitializeComponent(); | |
this.FlipOut.Completed += this.FlipOut_Completed; | |
this.FlipIn.Completed += this.FlipIn_Completed; | |
this.timer.Interval = TimeSpan.FromSeconds(rand.Next(1, 3)); | |
this.timer.Tick += this.timer_Tick; | |
this.timer.Start(); | |
} | |
public ImageSource FrontImage | |
{ | |
get | |
{ | |
return (ImageSource)this.GetValue(FrontImageProperty); | |
} | |
set | |
{ | |
this.SetValue(FrontImageProperty, value); | |
} | |
} | |
public ImageSource RearImage | |
{ | |
get | |
{ | |
return (ImageSource)this.GetValue(RearImageProperty); | |
} | |
set | |
{ | |
this.SetValue(RearImageProperty, value); | |
} | |
} | |
public void FrontImagePropertyChanged(DependencyPropertyChangedEventArgs e) | |
{ | |
this.Image1.Source = (ImageSource)e.NewValue; | |
} | |
public void RearImagePropertyChanged(DependencyPropertyChangedEventArgs e) | |
{ | |
this.Image2.Source = (ImageSource)e.NewValue; | |
} | |
private static void OnFrontImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
((TileControl)d).FrontImagePropertyChanged(e); | |
} | |
private static void OnRearImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
((TileControl)d).RearImagePropertyChanged(e); | |
} | |
private void FlipIn_Completed(object sender, object e) | |
{ | |
this.flipped = true; | |
this.timer.Interval = TimeSpan.FromSeconds(rand.Next(3, 12)); | |
this.timer.Start(); | |
} | |
private void FlipOut_Completed(object sender, object e) | |
{ | |
this.flipped = false; | |
this.timer.Interval = TimeSpan.FromSeconds(rand.Next(3, 12)); | |
this.timer.Start(); | |
} | |
private void timer_Tick(object sender, object e) | |
{ | |
this.timer.Stop(); | |
if (!this.flipped) | |
{ | |
this.FlipIn.Begin(); | |
} | |
else | |
{ | |
this.FlipOut.Begin(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment