Click here to Skip to main content
15,881,635 members
Articles / Desktop Programming / XAML

Silverlight Advanced View Model Style Video Player

Rate me:
Please Sign up or sign in to vote.
4.91/5 (35 votes)
16 May 2010Ms-PL12 min read 83.4K   4K   48  
A full featured Silverlight View Model Style Video Player
<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="clr-namespace:MVVMVideoPlayer_Test"
	xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
	xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
	xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="userControl" mc:Ignorable="d"
	x:Class="MVVMVideoPlayer_Test.MainPage" d:DesignWidth="600" d:DesignHeight="350">
	<UserControl.Resources>
		<DataTemplate x:Key="StringTemplate">
			<StackPanel>
				<TextBlock Text="{Binding Mode=OneWay}"/>
			</StackPanel>
		</DataTemplate>
		<DataTemplate x:Key="StringTemplate1">
			<StackPanel>
				<TextBlock Text="{Binding Mode=OneWay}"/>
			</StackPanel>
		</DataTemplate>
	</UserControl.Resources>

	<Grid x:Name="LayoutRoot" Background="White">
		<Grid.RowDefinitions>
			<RowDefinition Height="0.8*"/>
			<RowDefinition Height="0.2*"/>
		</Grid.RowDefinitions>
		<i:Interaction.Triggers>
			<i:EventTrigger EventName="SizeChanged">
				<i:InvokeCommandAction Command="{Binding ExitFullScreenCommand}" CommandParameter="{Binding ElementName=FullScreenGrid, Mode=OneWay}"/>
			</i:EventTrigger>
		</i:Interaction.Triggers>
		<Grid.DataContext>
			<local:MainViewModel/>
		</Grid.DataContext>
		<TextBlock x:Name="ProgressDisplay" Text="{Binding CurrentProgressProperty}" TextAlignment="Center" Margin="195,0,220,12" VerticalAlignment="Bottom" Height="12" RenderTransformOrigin="0.5,0.5" d:LayoutOverrides="HorizontalAlignment">
			<TextBlock.RenderTransform>
				<CompositeTransform/>
			</TextBlock.RenderTransform>
		</TextBlock>
		<TextBlock x:Name="VideoName" Text="{Binding SelectedVideoProperty}" Margin="31,47,32,0" VerticalAlignment="Top" TextAlignment="Center"/>
		<MediaElement x:Name="mediaElement" Margin="71,78,97,82" Source="{Binding SelectedVideoProperty}" AutoPlay="False">
			<i:Interaction.Triggers>
				<i:EventTrigger EventName="MediaOpened">
					<i:InvokeCommandAction Command="{Binding MediaOpenedCommand}" CommandParameter="{Binding ElementName=mediaElement, Mode=OneWay}"/>
					<i:InvokeCommandAction Command="{Binding SetSeekControlCommand}" CommandParameter="{Binding ElementName=progressBar}"/>
				</i:EventTrigger>
				<i:EventTrigger EventName="MouseLeftButtonDown">
					<i:InvokeCommandAction Command="{Binding SetFullScreenCommand}" CommandParameter="{Binding ElementName=FullScreenGrid, Mode=OneWay}"/>
				</i:EventTrigger>
			</i:Interaction.Triggers>
		</MediaElement>
		<Grid x:Name="PlayerControls" VerticalAlignment="Top" Grid.Row="1" >
			<Canvas VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,361,0">
				<Slider Maximum="1" LargeChange="0.5" Value="{Binding Volume, ElementName=mediaElement, Mode=TwoWay}" Width="49" HorizontalAlignment="Left" Canvas.Left="343" Height="22"/>
				<Button x:Name="RewindButton" Content="&lt;&lt;" Width="27" HorizontalAlignment="Left" Canvas.Left="-32">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding RewindVideoCommand}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
				<Button x:Name="PlayButton" Content="Play" Width="75" HorizontalAlignment="Left">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding PlayVideoCommand}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
				<Button x:Name="StopButton" Content="Stop" Width="68" Canvas.Left="158" HorizontalAlignment="Left">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding StopVideoCommand}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
				<Button x:Name="FullScreenButton" Content="Full Screen" Width="75" HorizontalAlignment="Left" Canvas.Left="230">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding SetFullScreenCommand}" CommandParameter="{Binding ElementName=FullScreenGrid, Mode=OneWay}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
				<Button x:Name="PauseButton" Content="Pause" Width="75" Canvas.Left="79" HorizontalAlignment="Left">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding PauseVideoCommand}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
				<Button x:Name="ForwardButton" Content="&gt;&gt;" Width="27" HorizontalAlignment="Left" Canvas.Left="309">
					<i:Interaction.Triggers>
						<i:EventTrigger EventName="Click">
							<i:InvokeCommandAction Command="{Binding ForwardVideoCommand}"/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
				</Button>
			</Canvas>
		</Grid>
		<ProgressBar x:Name="progressBar" Height="16" Margin="193,0,219,29" VerticalAlignment="Bottom" Maximum="{Binding TotalDurationProperty}" Value="{Binding CurrentPositionProperty, Mode=TwoWay}"/>
		<ComboBox x:Name="VideoList" Margin="206,0,237,58" VerticalAlignment="Bottom" ItemsSource="{Binding SilverlightVideoList}" SelectedIndex="{Binding SelectedVideoInListProperty}">
			<i:Interaction.Triggers>
				<i:EventTrigger EventName="DropDownClosed">
					<i:InvokeCommandAction Command="{Binding SetVideoCommand}" CommandParameter="{Binding SelectedItem, ElementName=VideoList}"/>
				</i:EventTrigger>
			</i:Interaction.Triggers>
		</ComboBox>
		<toolkit:BusyIndicator Content="" Margin="229,156,260,0" VerticalAlignment="Top" IsBusy="{Binding MediaBufferingProperty}" Height="58" BusyContent="{Binding MediaBufferingTimeProperty}"/>
		<Grid x:Name="FullScreenGrid" Margin="0" Grid.RowSpan="2"/>
	</Grid>
</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions