我不太明白为什么它如此复杂,因为在标准的Windows窗体中,下面的代码工作得很好。但无论如何。
我试图将图像淡入淡出,然后将其淡出。目前我甚至无法让它淡入,我觉得很愚蠢,因为我确信有些事我做错了。 for循环有效但图像不透明度在达到99之前不会改变然后突然改变。请帮助,因为这让我很生气。
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
for (int i = 1; i <+ 100; i++)
{
Logo.Opacity = i;
label1.Content = i;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 10);
dispatcherTimer.Start();
}
}
}
答案 0 :(得分:2)
我不确切知道你想要获得什么样的行为,但在WPF中你应该使用动画。可能你必须调整参数:
private void Button_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation
{
From = 0,
To = 1,
Duration = new Duration(TimeSpan.FromSeconds(1)),
AutoReverse = true
};
Logo.BeginAnimation(OpacityProperty, da);
}
答案 1 :(得分:1)
不透明度为double
,范围为0.0 - 1.0。所以循环应该是这样的。
for (double i = 0.0; i <= 1.0; i+=0.01)
{
Logo.Opacity = i;
label1.Content = i;
}
但正如克莱门斯所指出的那样,它也不会起作用。你在一次短暂的爆发中做了整个循环。你应该每个计时器滴答一次增加:
double CurrentOpacity = 0.0;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
CurrentOpacity += 0.01;
if(CurrentOpacity <= 1.0)
{
Logo.Opacity = CurrentOpacity;
label1.Content =CurrentOpacity;
}
else
{
dispatcherTimer.Stop();
}
}