所以我试图做一个简单的函数来淡入和淡出UIElement 出现的问题是该元素逐渐淡化并具有1的不透明度,但是在淡出时,不透明度在大约0.01处变为0相对结束。
public static async Task<bool> FadeIO(UIElement target, int FadeTime = 100, int DelayBeforeFade = 5000, int DelayBeforeOutFade = 10000)
{
double OpacTick = 0; //The "counter" for the while loops.
double FadeAmount = ((double)1 / FadeTime); //Calculates the required opacity increment for the fade to happen in the specified time.
await Task.Delay(DelayBeforeFade); //Holds until the required delay before the fade has been reached.
do
{
target.Opacity = 0 + OpacTick; //Alters the target's opacity based on the current loop cylce.
OpacTick += FadeAmount; //Alters the counter by the pre calculated alteration amount.
await Task.Delay(1); //Halts the loop
} while (OpacTick <= 1); //Loops finished when the target's opacity is 1.
OpacTick = 0; //Resets the loop counter.
await Task.Delay(DelayBeforeOutFade); //Holds until the required delay before the fade out has been reached
do
{
target.Opacity = 1 - OpacTick; //Alters the target's opacity based on the current loop cylce.
OpacTick += FadeAmount; //Alters the counter by the pre calculated alteration amount.
await Task.Delay(1); //Halts the loop
} while (OpacTick <= 1); //Loops finished when the target's opacity is 0 and the counter is therefor 1.
return true;
}
这个问题最奇怪的是,在不同的系统上进行测试时,不透明度可以完全恢复为0,而在某些系统上,不透明度可以达到大约0.01
答案 0 :(得分:1)
最后一次循环迭代不会使您达到0:
将不透明度设置为〜0.01,然后然后递增OpacTick
,然后将其与1进行检查。它实际上从未将不透明度设置为递增量。
话虽如此,这完全是不必要的。 DoubleAnimationUsingKeyFrames
可以为您提供更好的服务。