[英]Action executing only when stepping with debugger
Using the WP8 emulator, I have Media Element (inside the Layout Root of my XAML file, so the element is part of the visual tree at runtime), and I'm trying to programmatically trigger it to play from the code behind. 使用WP8仿真器,我有了Media元素(在我的XAML文件的Layout Root里面,因此该元素在运行时是视觉树的一部分),并且我试图以编程方式触发它从后面的代码中播放。
I'm using the Caliburn Micro EventAggregator to send a message when something in my app's backend requires a sound to be played. 当我的应用后端需要播放声音时,我正在使用Caliburn Micro EventAggregator发送消息。 In the view's code behind I'm using IHandle to set the media element's Source and trigger Play. 在后面的视图代码中,我使用IHandle设置媒体元素的Source并触发Play。
public void Handle(ToneMessage message)
{
MediaElem.Source = message.ToneUri;
MediaElem.Play();
}
When I trigger the message event, no sound is played. 当我触发消息事件时,没有声音播放。 However, if I put a breakpoint on MediaElem.Play()
, when I step over it, the sound plays. 但是,如果我在MediaElem.Play()
上放置一个断点,则当我越过它时,声音会播放。
I don't know what's happening, basically my code works only when I'm stepping over it with the debugger. 我不知道发生了什么,基本上我的代码只有在使用调试器进行调试时才有效。 I'm still on the UI thread (even tried explicitly using the Dispatcher). 我仍然在UI线程上(甚至使用Dispatcher进行了明确尝试)。
Any ideas are welcomed. 任何想法都欢迎。
If it works with the debugger and not in the actual application, you can be almost certain it's a timing issue. 如果它与调试器一起使用,而不是在实际应用程序中使用,则几乎可以肯定这是一个时序问题。
In your case, the problem is that you're not waiting for the sound to load before trying to play it. 就您而言,问题在于您在尝试播放声音之前没有等待声音加载。 It works on the emulator because the execution is paused, giving enough time to load the sound. 它可以在模拟器上运行,因为执行被暂停了,从而有足够的时间来加载声音。
Before setting the source, subscribe to the MediaOpened
event of the MediaElement. 在设置源之前,请订阅MediaElement的MediaOpened
事件。 Then, in that event, call the Play
method to play the sound. 然后,在这种情况下,调用Play
方法播放声音。
Alternatively, you can set the AutoPlay
property to true
to automatically play the sound once it has finished downloading. 或者,可以将AutoPlay
属性设置为true
以在完成下载后自动播放声音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.