淡化模糊元素

Fade blur element

所以我有 WPF 应用程序,在某个时候当我的应用程序做它的事情时,我想 blur 我的网格与我的所有控制器在特定时间然后返回,所以目前我正在使用这个方法:

private void BlurElement(Grid grid, double value)
{
    Grid g = grid;
    BlurBitmapEffect blurEffect = new BlurBitmapEffect();
    blurEffect.Radius = value;
    g.BitmapEffect = blurEffect;
}

用法

BlurElement(myGrid, 30);

返回:BlurElement(myGrid, 0);

所以这很好用,但现在我希望这种模糊与淡入淡出元素一起出现,所以我创建了这个计时器:

private DispatcherTimer fadeTimer;

fadeTimer = new DispatcherTimer();
fadeTimer.Tick += FadeTimer_Tick;
fadeTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);

从我的 Timer 打勾:

private void FadeTimer_Tick(object sender, EventArgs e)
{
    BlurElement(myGrid, count += 1);
    if (counter == 30)
        fadeTimer.Stop();
}

然后慢慢做。

所以我的问题是,如果没有这个 Timer 或某些库(我主要使用 Windows7)是否有更简单的方法来实现这一点

除了在每个计时器刻度上创建新的 BlurBitmapEffect 效率低下之外,WPF BitmapEffects 是可动画的。所以你可以简单地在 XAML

中声明效果
<Grid>
    <Grid.BitmapEffect>
        <BlurBitmapEffect x:Name="blurBitmapEffect" Radius="0"/>
    </Grid.BitmapEffect>
    ...
</Grid>

并在代码后面的某处开始动画:

blurBitmapEffect.BeginAnimation(
    BlurBitmapEffect.RadiusProperty,
    new DoubleAnimation(30, TimeSpan.FromSeconds(3)));

或者只做 XAML 中的所有操作,例如像这样:

<Grid.BitmapEffect>
    <BlurBitmapEffect Radius="0"/>
</Grid.BitmapEffect>
<Grid.Triggers>
    <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetProperty="BitmapEffect.Radius"
                    To="30" Duration="0:0:3"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Grid.Triggers>