public async Task<int> CreateCloud(bool isInitial) {
var cloud = new Image {
Source = new BitmapImage(new Uri("ms-appx:/Assets/BackgroundClouds.png")),
Width = Random.Next(800, 2000),
Stretch = Stretch.Uniform,
Visibility = Visibility.Collapsed
};
Children.Add(cloud);
await cloud.WaitForNonZeroSizeAsync();
var cloudYMax = (int)(ActualHeight - cloud.ActualHeight / 2);
var cloudY = TopMargin > cloudYMax
? TopMargin
: Random.Next(TopMargin, cloudYMax);
SetTop(cloud, cloudY);
SetZIndex(cloud, (int)cloud.Width);
var time = BaseTime * 1000 + (int)(Random.NextDouble() * RandomTime * 1000);
var from = (int)(-cloud.Width);
var to = (int)ActualWidth;
SetLeft(cloud, from);
var storyboard = new Storyboard();
var animation = new DoubleAnimation {
AutoReverse = false,
Duration = new Duration(TimeSpan.FromMilliseconds(time)),
RepeatBehavior = new RepeatBehavior(1),
From = from,
To = to,
EnableDependentAnimation = true,
};
animation.Completed += delegate {
Children.Remove(cloud);
};
storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, cloud);
Storyboard.SetTargetProperty(animation, "(Canvas.Left)");
Resources.Add(Guid.NewGuid().ToString(), storyboard);
storyboard.Begin();
if (isInitial) {
storyboard.Seek(TimeSpan.FromMilliseconds(Random.Next(0, time)));
}
cloud.Visibility = Visibility.Visible;
return time;
}