Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C# User control page IsVisibleChanged event

i have some user control page , and a MainWindow , so i would like to set user control page to hidden , and once its hidden , it stops its audio from playing

i know i have to do something with IsVisibleChanged event but i got stuck at how to start it?

Is it something like this ?

(pagename).IsVisibleChanged(object sender , RoutedEventArgs e )
 {    ap.Stop()  }

Because my user control page ( i display it within the mainwindow using a custom control ) is in my mainwindow and the user control page have some audio playing , when i click the home button that resides on the mainwindow , i'll set the user control page to hidden and show my home page , but now when it is hidden , the audio from that page is still playing , so i went to ask and some said use IsVisibleChanged event in user control page ( the 1 that plays the audio ) but i got stuck at how do i even write it cos i am new to this .

like image 672
what Avatar asked Jul 21 '13 09:07

what


People also ask

Is WPF and C# same?

C# is a programming language. WPF is a technology used to develop rich GUI applications using C# (or any other . NET language).

Is WPF still relevant 2022?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.

What is WPF in C# used for?

Windows Presentation Foundation (WPF) is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security.

What replaced WPF?

Universal Windows Platform. Both Windows Forms and WPF are old, and Microsoft is pointing developers towards its Universal Windows Platform (UWP) instead. UWP is an evolution of the new application platform introduced in Windows 8 in 2012.


1 Answers

First solution:

You should use DependencyPropertyChangedEventArgs as second argument and you should check NewValue property that indicates if page will be visible or not (msdn).

Example:

void (pagename)_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (!((bool)e.NewValue))
    {
        ap.Stop();
    }
}

Here you find the sample solution (IsVisibleChangedExample).

Second solution:

If you use MediaElement (msdn) to play the music, you should use two properties: LoadedBehavior (msdn) and UnloadedBehavior (msdn).

Example:

<MediaElement Name="me" Source="path to your music file" 
              LoadedBehavior="Play" UnloadedBehavior="Stop" Volume="100" />
like image 188
kmatyaszek Avatar answered Sep 22 '22 21:09

kmatyaszek