简体   繁体   English

UI无法更新foreach循环C#WPF中首先播放的声音

[英]UI not updating sounds played first in foreach loop C# WPF

I have made a card game that gets and shows player cards from resources in a UI using a foreach loop which works fine. 我制作了一种纸牌游戏,可以使用foreach循环从UI中的资源获取并显示玩家纸牌,效果很好。 I have recently added a card sound as well so that a sound would be played before each image is shown in the UI. 我最近还添加了卡声音,以便在UI中显示每个图像之前先播放声音。 However, the sounds are always played first and then the images are shown. 但是,总是先播放声音,然后再显示图像。 A bit annoying... 有点烦人...

I use sound player and playsync(). 我使用声音播放器和playsync()。 How do I force the GUI to update? 如何强制GUI更新? (between the sounds) (在声音之间)

Here is some of the code... 这是一些代码...

Starts with 以。。开始

//loop
foreach (var player in playerGUIs)....
//Call the sound Card_Sound(); //See below

//Update image player.img1.Source = new BitmapImage(new
Uri(("/Utilities;Component/Resources/" + card_file + ".png"),
UriKind.Relative));


private void Card_Sound()
    { //Play sound System.Threading.Thread.Sleep(800); // First sleep 800 milliseconds

using (FileStream stream = File.Open(@"Resources\dealingcard.wav",
FileMode.Open)) {
    SoundPlayer myNewSound = new SoundPlayer(stream);
           myNewSound.Load();
           myNewSound.PlaySync(); }

}

您是否尝试过Play(),PlaySync()会阻止UI(如果在UI线程上运行)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM