Universal Windows Platform – Slide Player

Slide Player demonstrates how to use a Image control to display a sequence of images like a presentation

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Imaging;

public class Library
{
    public delegate void PlayingEvent(BitmapImage image, int index);
    public event PlayingEvent Playing;
    public delegate void StoppedEvent();
    public event StoppedEvent Stopped;

    private List<BitmapImage> _list = new List<BitmapImage>();
    private int _index = 0;
    private bool _paused = false;

    public bool IsPlaying { get; set; }
    public int Speed { get; set; }
    public int Position { get; set; }

    public void Go(ref Image display, string value, KeyRoutedEventArgs args)
    {
        if (args.Key == Windows.System.VirtualKey.Enter)
        {
            try
            {
                display.Source = new BitmapImage(new Uri(value));
            }
            catch
            {

            }
        }
    }

    public double Add(string value)
    {
        _list.Add(new BitmapImage(new Uri(value)));
        return _list.Count - 1;
    }

    public double Remove(int index)
    {
        if (index >= 0 && index < _list.Count)
        {
            _list.RemoveAt(index);
        }
        return _list.Count - 1;
    }

    public async void Play()
    {
        if (_list.Any() && (!_paused || !IsPlaying))
        {
            IsPlaying = true;
            _paused = false;
            while (IsPlaying)
            {
                if (_list.Count > 0)
                {
                    if (_index < _list.Count)
                    {

                        Playing(_list[_index], _index);
                        _index += 1;
                    }
                    else
                    {
                        this.Stop();
                    }
                }
                await Task.Delay(Speed);
            }
        }
    }

    public void Pause()
    {
        if (_list.Any() && IsPlaying)
        {
            _paused = true;
            IsPlaying = false;
        }
    }

    public void Stop()
    {
        if (_list.Any())
        {
            _index = 0;
            _paused = false;
            IsPlaying = false;
            Stopped();
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. There’s a PlayingEvent and Stopped Event to indicate when those states occur in the application. The slides are represented by a List of BitmapImage with some properties to represent IsPlaying, Speed and Position. The Go Method handles when a URL of an image is typed in, the Image will be set to this for preview purposes. The Add and Remove Methods are used to insert or remove the Slides from the presentation being made up of BitmapImage. The Play Method is used to initate the slide show and start playing through the List of BitmapImage, Pause is used to pause the slideshow and Stop is used to stop it.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Margin="20" Name="Value" InputScope="Url" KeyDown="Go_KeyDown"/>
	<Grid Grid.Row="1" Margin="5">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="120"/>
		</Grid.ColumnDefinitions>
		<Slider Grid.Column="0" Padding="5" Minimum="0" Name="Position" ValueChanged="Position_ValueChanged"/>
		<Slider Grid.Column="2" Padding="5" Minimum="100" Maximum="5000" Value="4000" Name="Speed" ValueChanged="Speed_ValueChanged"/>
	</Grid>
	<Image Grid.Row="2" Margin="75" Name="Display"/>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Add" Label="Add" Click="Add_Click"/>
	<AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/>
	<AppBarButton Name="Play" Icon="Play" Label="Play" Click="Play_Click"/>
	<AppBarButton Icon="Stop" Label="Stop" Click="Stop_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a Grid with three rows, the first row is for the TextBox where the URL of an image may be entered, the second row is for a pair of Slider Controls for the Position in the Slideshow and the other is for the Speed of the slideshow. The third row is for the Image where an image can be previewed or displayed. The second block of XAML is is the CommandBar which contains the Add, Remove, Play and Stop commands to perform the actions of the Slide Player using the Image control along with the Slider Controls.

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	library.Speed = (int)Speed.Value;
	library.Playing += (Windows.UI.Xaml.Media.Imaging.BitmapImage image, int index) =>
	{
		Display.Source = image;
		Position.Value = index;
	};
	library.Stopped += () =>
	{
		Play.Icon = new SymbolIcon(Symbol.Play);
		Play.Label = "Play";
		Display.Source = null;
		Position.Value = 0;
	};
}

private void Go_KeyDown(object sender, KeyRoutedEventArgs e)
{
	library.Go(ref Display, Value.Text, e);
}

private void Position_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
	library.Position = (int)Position.Value;
}

private void Speed_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
	if (Speed != null)
	{
		library.Speed = (int)Speed.Value;
	}
}

private void Add_Click(object sender, RoutedEventArgs e)
{
	Position.Maximum = library.Add(Value.Text);
}

private void Remove_Click(object sender, RoutedEventArgs e)
{
	Position.Maximum = library.Remove((int)Position.Value);
}

private void Play_Click(object sender, RoutedEventArgs e)
{
	if (library.IsPlaying)
	{
		library.Pause();
		Play.Icon = new SymbolIcon(Symbol.Play);
		Play.Label = "Play";
	}
	else
	{
		library.Play();
		Play.Icon = new SymbolIcon(Symbol.Pause);
		Play.Label = "Pause";
	}
}

private void Stop_Click(object sender, RoutedEventArgs e)
{
	library.Stop();
}

Below the MainPage() Method an instance of the Library Class is created. The OnNavigatedTo is triggered when the MainPage is loaded and this will setup the slideshow. The Position_ValueChanged and Speed_ValueChanged allow the current position and speed of the slideshow to be controlled. Add_Click and Remove_Click allow images to be added or removed from the slideshow. Play_Click and Stop_Click allow the slideshow to be started or stopped.

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

After the Application has started running you can then type in the URL of any image e.g. e.g. https://dummyimage.com/300×200/000000/ffffff&text=Hello+World then press or tap enter to load this then use Add to create a Slide from this, you can then use multiple images to build up a slide show which can be Played with Play

ran-slide-player

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s