Mega Code Archive
Use Window Activated and Deactivated event to control a media file
//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
public partial class CustomMediaPlayerWindow : Window
{
public CustomMediaPlayerWindow()
{
InitializeComponent();
}
bool isMediaElementPlaying;
void playButton_Click(object sender, RoutedEventArgs e) {
this.mediaElement.Play();
this.isMediaElementPlaying = true;
}
void stopButton_Click(object sender, RoutedEventArgs e)
{
this.mediaElement.Stop();
this.isMediaElementPlaying = false;
}
void window_Activated(object sender, EventArgs e)
{
if( this.isMediaElementPlaying ) this.mediaElement.Play();
}
void window_Deactivated(object sender, EventArgs e)
{
if (this.isMediaElementPlaying) this.mediaElement.Pause();
}
void exitMenu_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
void window_Closing(object sender, CancelEventArgs e)
{
if (this.isMediaElementPlaying)
{
e.Cancel = true;
}
}
}