简体   繁体   English

仅在使用调试器执行操作时执行

[英]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.

相关问题 Jtable仅在逐步调试器时显示 - Jtable only displays when stepping through debugger 最小化窗口功能仅在调试器(C#-Console)中逐步调试时有效 - Minimize Window function only works when stepping through in Debugger (C# - Console) 进入调试器时,Visual Studio 2008,Excel加载项挂起 - Visual Studio 2008, excel add-in hangs when stepping in debugger 仅当我从组合框中选择项目时才执行操作 - Executing an action only when I select an item form the combo box 调试器没有跨过/进入异步/等待 - Debugger not stepping over/into async/wait 调试器步进if语句和lambda表达式 - Debugger stepping in if statement and lambda expression 检测调试器是否已连接*和*单步执行 - Detect if Debugger is Attached *and* stepping through 更新集合时出现奇怪的DB上下文行为(在调试器中逐步执行代码时有效) - Strange DB-context behaviour when updating collection (Works when stepping through the code in debugger) Debug.Assert()仅在单步执行时触发 - Debug.Assert() only triggers when stepping over it 我单步执行程序时只会得到Stackoverflow异常 - I Only get a Stackoverflow exception when stepping through program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM