【发布时间】:2017-04-05 21:05:34
【问题描述】:
我的代码中目前有四个 if 语句来更改某些显示。在InQueueInRing > 10 的if 语句中,我希望它在红色、黄色和绿色图像之间每0.5 秒循环一次,直到条件不再成立。我不知道从哪里开始骑自行车图像。我是否只是多次列出图像源并在它们之间加上Thread.Sleep(1000)?
我尝试使用Loading new image each second 但我无法将类型 system.threading.timer 转换为 system.windows.media.imagesource。下面更新了代码。
以下是我的if 声明
if (e.CmsData.Skill.AgentsAvailable > 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
callsWaitingText.Text = "Available";
callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_green.png", UriKind.Absolute));
}));
}
else if (e.CmsData.Skill.InQueueInRing > 10)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
callsWaitingText.Text = "Waiting";
timer = new system.threading.timer(OnTImerEllapsed, new object(), 0, 2000);
}));
}
else if (e.CmsData.Skill.InQueueInRing > 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
callsWaitingText.Text = "Waiting";
callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_red.png", UriKind.Absolute));
}));
}
else if (e.CmsData.Skill.AgentsAvailable == 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
callsWaitingText.Text = "Available";
callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png", UriKind.Absolute));
}));
}
private void OnTimerEllapsed(object state)
{
if (!this.Dispatcher.CheckAccess())
{
this.Dispatcher.Invoke(new Action(LoadImages));
}
}
private void LoadImages()
{
string stringUri = switcher ? @"pack://application:,,,/ScoreBoardClientTest;component/images/circle_red.png" :
@"pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png";
// @"pack://application:,,,/ScoreBoardClientTest;component/images/circle_green.png";
this.callimgae.Source = new BitmapImage(new Uri(stringUri));
switcher = !switcher;
}
【问题讨论】:
-
用我找到的另一篇文章的新尝试更新了代码。
-
还要注意,在我的尝试中,我尝试使用所有三个图像,但由于错误指出只能使用赋值、调用、递增、递减和新对象表达式,我不得不注释掉最后一个用作陈述。即使它不是一个声明并且格式正确。
-
好的,我想我自己通过反复试验解决了这个问题。正确答案是问题,一旦堆栈溢出允许我这样做,我将自己标记为已回答。
标签: c#