5

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 .

4

1 回答 1

15

第一个解决方案:

您应该将DependencyPropertyChangedEventArgs其用作第二个参数,并且您应该检查NewValue指示页面是否可见的属性(msdn)。

例子:

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

在这里您可以找到示例解决方案 (IsVisibleChangedExample)。

第二种解决方案:

如果您使用MediaElement( msdn ) 播放音乐,则应使用两个属性: LoadedBehavior( msdn ) 和UnloadedBehavior( msdn )。

例子:

<MediaElement Name="me" Source="path to your music file" 
              LoadedBehavior="Play" UnloadedBehavior="Stop" Volume="100" />
于 2013-07-21T10:05:26.783 回答