Chapter 10: Designing Silverlight Applications

293

m_isPlaying = true;

}

}

}

}

VB:

Partial Public Class MainPage

Inherits UserControl

Public Sub New()

InitializeComponent()

VideoPlayer.AutoPlay = False

End Sub

Dim m_isPlaying As Boolean = False

Private Sub StartStopButton_Click(

ByVal sender As System.Object,

ByVal e As System.Windows.RoutedEventArgs)

If (m_isPlaying) Then

VideoPlayer.Stop()

StartStopButton.Content = "Start"

m_isPlaying = False

Else

VideoPlayer.Play()

StartStopButton.Content = "Stop"

m_isPlaying = True

End If

End Sub

End Class

By default, the MediaElement starts playing the Source video as soon as the application loads, so I set AutoPlay to false in the code-behind constructor. The m_isPlaying field keeps track of whether the MediaElement is playing or not. The Click event handler uses m_isPlaying to toggle between playing and stopped.

This is a quick demo of how to work with the MediaElement control, but there’s much more you can do, such as pausing, tracking buffering, checking video position, and more. All you need to do is either capture events of the MediaElement control or use controls like buttons and sliders to interact with MediaElement, as the example shows in Listing 10-2. It would be good practice for you to take what you’ve learned here and add more functionality to the MediaElement control.

Page 316
Image 316
Microsoft 9GD00001 manual 293, Dim misPlaying As Boolean = False