Search code examples
c#wpfxamlanimated-gifmediaelement

How to disable media element (.gif) while program is running


I have a media element in my wpf application which includes a loading gif. When the user selects a long running process, it basically displays a loader gif to let the user know that the process is running. What I am trying to do is to disable it once the long running process has stopped running. I have included the XAML and process ended event for it below. Also, the main part, this is how I am starting the gif and stopping the gif. I might be wrong there.

    loadGif.Play();

// Long running process(s)

    loadGif.Pause();
    loadGif.Close();

XAML BELOW:

 <MediaElement Name="loadGif" HorizontalAlignment="Left" LoadedBehavior="Manual" MediaEnded="process_Ended" Height="22" Margin="609,226,0,0" VerticalAlignment="Top" Width="31" RenderTransformOrigin="-0.258,0.523" Source="C:\\ajax-loader.gif"/>

Process Ended:

private void process_Ended(object sender, RoutedEventArgs e)
        {
            loadGif.Position = new TimeSpan(0, 0, 1);
            loadGif.Play();
        }

Also, I forgot to mention what happens right now, at the moment, the gif starts when its supposed to start and after the process(s) are done, it disappears and a black square takes its spot. Since the background of my application is white, its just a small black box sitting there after the processing is done. Essentially, the media element spot goes all black. Thanks for any help!


Solution

  • Set the Visibility of the MediaElement to either Collapsed or Hidden, depending on how you want the layout to work.

    loadGif.Visibility = Visibility.Collapsed;