我有以下场景:当用户将鼠标移出弹出窗口时,我想要动画发生,五秒钟之后,我想删除一个PopUp。
这是我期望这样做的代码是:
private bool leftPopup = false; public void AnimatePopupOut(object sender, MouseEventArgs e) { myAnim.Begin(); (new Thread(new ThreadStart(delayedRemovePopup))).Start(); } private void delayedRemovePopup() { leftPopup = true; Thread.Sleep(5000); PopUp.IsOpen = false; }第一行“leftPopup = true”很好,但第三行“PopUp.IsOpen = false”给了我一个访问冲突异常,可能是因为这个对象属于GUI线程。 有什么方法可以访问PopUp.IsOpen属性吗? 如果没有,是否有其他方式可以在一段时间后调用事件来执行此操作?
干杯
聂
I've got the following scenario: when a user moves the mouse out of a popup, I want an animation to happen, and five seconds later, I want to remove a PopUp.
This is the code I expected to do this with is:
private bool leftPopup = false; public void AnimatePopupOut(object sender, MouseEventArgs e) { myAnim.Begin(); (new Thread(new ThreadStart(delayedRemovePopup))).Start(); } private void delayedRemovePopup() { leftPopup = true; Thread.Sleep(5000); PopUp.IsOpen = false; }The first line, "leftPopup = true" is fine, but the third, "PopUp.IsOpen = false" gives me an access violation exception, probably because this object belongs to the GUI thread. Is there any way I can gain access to the PopUp.IsOpen property? If not, is there another way that I can call an event after some time to do this?
Cheers
Nik
最满意答案
尝试使用PopUp.Dispatcher.Invoke()。 这将把你的回调编组回UI线程。
Try using the PopUp.Dispatcher.Invoke(). That will marshal your call back to the UI thread.
更多推荐
发布评论