// PlayNextImage // Called when a new image is displayed due to a timeout. // Removes the current image object and queues a new next image. // Sets the next image index as the new current image, and increases the size // of the new current image. Then sets the timeout to display the next image. private async void PlayNextImage(int num) { // Stop the timer to avoid repeating. if (timer != null) { timer.Stop(); } await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { SlideShowPanel.Children.Remove((UIElement)(SlideShowPanel.FindName("image" + num))); var i = await QueueImage(num + 2, false); currentImage = num + 1; ((Image)SlideShowPanel.FindName("image" + currentImage)).Width = imageSize; }); timer = new Windows.UI.Xaml.DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, timeLapse); timer.Tick += delegate(object sender, object e) { PlayNextImage(num + 1); }; timer.Start(); }
// QueueImage // Called to create an image object for the displayed images. private async System.Threading.Tasks.Task <Image> QueueImage(int num, bool isFirstImage) { // Create the image element for the specified image index and add to the // slide show div. Image image = new Image(); image.Width = isFirstImage ? imageSize : thumbnailSize; image.Name = "image" + num; image.VerticalAlignment = VerticalAlignment.Bottom; var fileContents = await imageList[num % imageList.Count].OpenReadAsync(); var imageBitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); imageBitmap.SetSource(fileContents); image.Source = imageBitmap; await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { SlideShowPanel.Children.Add(image); }); // If this is the first image of the slide show, queue the next image. Do // not queue if streaming as images are already queued before // streaming using Play To. if (isFirstImage && !streaming) { var i = await QueueImage(num + 1, false); timer = new Windows.UI.Xaml.DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, timeLapse); timer.Tick += delegate(object sender, object e) { PlayNextImage(num); }; timer.Start(); } // Use the transferred event of the Play To connection for the current image object // to "move" to the next image in the slide show. The transferred event occurs // when the PlayToSource.playNext() method is called, or when the Play To // Receiver selects the next image. image.PlayToSource.Connection.Transferred += async delegate(Windows.Media.PlayTo.PlayToConnection sender, Windows.Media.PlayTo.PlayToConnectionTransferredEventArgs e) { currentImage = num + 1; await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ((Image)SlideShowPanel.FindName("image" + currentImage)).Width = imageSize; }); }; // Use the statechanged event to determine which action to take or to respond // if the Play To Receiver is disconnected. image.PlayToSource.Connection.StateChanged += async delegate(Windows.Media.PlayTo.PlayToConnection sender, Windows.Media.PlayTo.PlayToConnectionStateChangedEventArgs e) { switch (e.CurrentState) { case Windows.Media.PlayTo.PlayToConnectionState.Disconnected: // If the state is disconnected and the current image index equals the // num value passed to queueImage, then the image element is not connected // to the Play To Receiver any more. Restart the slide show. // Otherwise, the current image has been discarded and the slide show // has moved to the next image. Clear the current image object and // remove it from the slide show div. if (currentImage == num) { await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { MessageBlock.Text = "Slideshow disconnected"; // Cancel any existing timeout if (timer != null) { timer.Stop(); } // Clear all image objects from the slide show div SlideShowPanel.Children.Clear(); // Reset the slide show objects and values to their beginning state streaming = false; DisconnectButton.Visibility = Visibility.Collapsed; InstructionsBlock.Visibility = Visibility.Visible; DisconnectButton.Click -= DisconnectButtonClick; // Restart the slide show from the current image index var i = await QueueImage(currentImage, true); }); } else { await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { image.PlayToSource.Next = null; if (streaming) { SlideShowPanel.Children.Remove(image); } }); } break; case Windows.Media.PlayTo.PlayToConnectionState.Connected: // If the state is connected and the previous state is disconnected, // then the image element is newly connected. Queue up the next image so // that it is loaded while the current image is being displayed. // If the previous state is rendering, then the user has paused the slideshow // on the Play To Receiver. Clear the current timeout until the user restarts // the slide show. await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { if (e.PreviousState == Windows.Media.PlayTo.PlayToConnectionState.Disconnected) { var imageNext = await QueueImage(num + 1, false); image.PlayToSource.Next = imageNext.PlayToSource; } else if (e.PreviousState == Windows.Media.PlayTo.PlayToConnectionState.Rendering) { if (timer != null) { timer.Stop(); } } if (currentImage == num) { MessageBlock.Text = "Slideshow connected"; } }); break; case Windows.Media.PlayTo.PlayToConnectionState.Rendering: // If the state is rendering and the previous state is // connected, then the Play To Receiver has restarted // the slide show. await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (e.PreviousState == Windows.Media.PlayTo.PlayToConnectionState.Connected) { // Clear any existing timeout. if (timer != null) { timer.Stop(); } // Restart the slide show. timer = new Windows.UI.Xaml.DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, timeLapse); timer.Tick += delegate(object s, object args) { image.PlayToSource.PlayNext(); }; timer.Start(); } if (currentImage == num) { MessageBlock.Text = "Slideshow rendering"; } }); break; } }; return(image); }