wpf按钮动画问题 [英] wpf button animation problem

查看:73
本文介绍了wpf按钮动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小应用程序我是新手wpf我正在尝试的是我有七个按钮,名为{按钮1,按钮2等等}不同颜色



他们的方法产生随机数{1,2,3,4}或{1,2}或其他组合



所以如果是随机数生成的是{1,2}所以按钮一应该显示动画然后2秒后按钮2应该显示动画



但是我面临的问题是两个按钮同时显示动画



有谁能告诉我如何解决它会非常友好



这是代码



i am trying to make a small application i am new to wpf what i am trying is i have seven buttons named as {button 1, button2 and so on} of different colors

theirs an method which generates random number {1,2,3,4} or {1,2} or someother combination

so if random numbers which are generated are {1,2} so button one should show animation and then after 2 seconds button 2 should should show animation

but the problem which i am facing is both the buttons are showing animation at same time

can anyone tell me how do i resolve it it will be very kind

this is the code

public delegate void AddOperationDelegate();
public partial class MainWindow : Window
{
private void InvokeMessage(IAsyncResult ar)
{
AddOperationDelegate aod = (ar as AsyncResult).AsyncDelegate as AddOperationDelegate;
aod.EndInvoke(ar);

}
DispatcherTimer dispatcherTimer = new DispatcherTimer();
List<list><int>> fnlst = new List<list><int>>();
List<int> gmequestion = new List<int>();
List<int> gmeanswer = new List<int>();

List<list><int>> chklist = new List<list><int>>();
public MainWindow()
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0);
dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{

GenreateNumbers();

}
public void GenreateNumbers()
{

List<int> ldata = new List<int>();
Random r = new Random();
int va = r.Next(1, 7);
int a1 = va / 10;
int b = va % 10;
if (a1==0)
{
a1 = a1 + 1;
}
Random rnd = new Random();
var randomNumbers = Enumerable.Range(a1, b).OrderBy(i => rnd.Next()).ToArray();
randomNumbers = (from i in randomNumbers.ToArray()
orderby i ascending
select i).ToArray();
List<int> c = new List<int>();
foreach (var item in randomNumbers)
{
if (item==1)
{
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button1.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\1.mp3");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 2)
{



DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button2.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\2.wav");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 3)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button3.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\3.wav");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 4)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button4.BeginAnimation(Button.WidthProperty, a);



}
else if (item == 5)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button5.BeginAnimation(Button.WidthProperty, a);
}
else if (item == 6)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button6.BeginAnimation(Button.WidthProperty, a);

}
else if (item == 7)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button7.BeginAnimation(Button.WidthProperty, a);
}
}
dispatcherTimer.Interval = new TimeSpan(0, 0, 15);



}

private void button1_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\1.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();


}
private void button2_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\2.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\3.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button4_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\4.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}

private void button5_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\5.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button6_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\6.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button7_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\7.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


public class CusComparer : IEqualityComparer<list><int>>
{
public bool Equals(List<int> x, List<int> y)
{
return x.SequenceEqual(y);
}

public int GetHashCode(List<int> obj)
{
int hashCode = 0;

for (var index = 0; index < obj.Count; index++)
{
hashCode ^= new { Index = index, Item = obj[index] }.GetHashCode();
}

return hashCode;
}
}

private void m7_MediaOpened(object sender, RoutedEventArgs e)
{

}

}

推荐答案

问题是您可能在UI线程中使用 Sleep 。你永远不应该这样,因为你不延迟动画,你只是延迟整个UI线程,使UI无响应。您应该使用嵌入在WPF中的动画,或者使用具有单独线程的低级动画(可以安全地 Sleep )并使用与UI线程进行通信调度员



请查看我过去的答案:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



-SA
The problem is that you are probably using Sleep in the UI thread. You should never to this, as you don't delay animation, you simply delay the whole UI thread, making UI unresponsive. You should either use the animation embedded in WPF or use low-level animation with a separate thread (which can Sleep safely) and communication with UI thread using Dispatcher.

Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


这篇关于wpf按钮动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆