问题
I have an app I'm porting from Windows Phone -> Windows 8 Store app. I play music via my App.Xaml.vb (so that it can play throughout my app and be controlled via CurrentApp). This all works great on the phone. When a music track ends, the MediaEnded event is fired and my app chooses another track and plays that. In Windows 8 when the first track ends the app goes silent.
On Windows 8 the MediaElement.MediaEnded event doesn't fire.
I set up my media element and add a handler:
Public WithEvents MusicElement as New MediaElement
[...then in the constructor...]
AddHandler MusicElement.MediaEnded, AddressOf ChangeTrack
The music plays (using a PlayMusic method which I don't think I need to post), but when the music ends the event isn't fired.
The difference between the phone version and the Windows 8 version is that I can't put the MediaElement control in the XAML in Win8 for some reason - it doesn't seem to be allowed, so I initialise it in my code behind.
I'm not using the 'background audio' media type (which I've heard can cause issues with the MediaEnded event).
I have confirmed that the handler is added before any playback is initiated.
Any ideas?
Edit: XAML (attempted) code in App.XAML:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="DetectiveStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--<MediaElement x:Name="MusicElement" x:Key="MusicElement" Source="/Sounds/Music/doing-my-best.mp3" AutoPlay="False"/>-->
</ResourceDictionary>
</Application.Resources>
回答1:
Is your MediaElement hooked up to the Visual Tree? If not, its event handlers generally will not get called.
Also, it is definitely possible to have MediaElement's defined in Xaml in Win8, if you add a snippet of the Xaml that's failing it should be possible to figure out what's wrong there.
来源:https://stackoverflow.com/questions/18142687/mediaelement-mediaended-event-not-firing