如何为任何控件设置 BlurEffect 动画?

发布于 2024-11-07 12:31:52 字数 339 浏览 3 评论 0原文

我想要一个可重用的函数,它只需对任何特定的有效控件类型执行 BlurEffect 即可。

例如,

var animation = new DoubleAnimation
{ 
   From = 0,
   To = 8,
   Duration = new TimeSpan(0, 0, 0, 2),
   AutoReverse = true
};
control.Effect = new BlurEffect();
// How to cycle the From and To on the Blur Radius?

我不确定如何在代码隐藏中动态创建动画以使其成为可能。

I would like a reusable function that simply does a BlurEffect on any particular valid Control type.

e.g.

var animation = new DoubleAnimation
{ 
   From = 0,
   To = 8,
   Duration = new TimeSpan(0, 0, 0, 2),
   AutoReverse = true
};
control.Effect = new BlurEffect();
// How to cycle the From and To on the Blur Radius?

I am uncertain how to dynamically create an animation in code behind to make this possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-11-14 12:31:52

本文展示了如何在代码中定义动画和故事板。

您需要添加 BlurEffect 并使用 RadiusProperty 的blureffect 而不是RotateTransform.AngleProperty,如下所示:

control.Effect.BeginAnimation(Blureffect.RadiusProperty, animation);

-编辑-

我现在看到您正在使用Silverlight,而不是wpf(其中效果实现 IAnimatable)
我会尝试找到 silverlight 的解决方案

-edit2-

你见过/尝试过 这个? Msdn 还有一个 示例for silverlight

-edit3-

你的指导让我找到了正确的答案,如下所示:

private void BlurSomething(FrameworkElement control)
        {
            var storyboard = new Storyboard();
            var animation = new DoubleAnimation
            {
                From = 0,
                To = 8,
                Duration = new TimeSpan(0, 0, 0, 1, 0),
                AutoReverse = true
            };
            var effect = new BlurEffect();
            control.Effect = effect;
            storyboard.Children.Add(animation);
            Storyboard.SetTarget(storyboard, control.Effect);
            Storyboard.SetTargetProperty(storyboard, new PropertyPath("Radius"));
            storyboard.Begin();
        }

应该注意的是,Silverlight 并不像 WPF 那样简单地制作动画,但最终这是可以做到的。

This article shows how to define an animation and story board in code.

You'd need to add the BlurEffect and use RadiusProperty of blureffect instead of RotateTransform.AngleProperty like this:

control.Effect.BeginAnimation(Blureffect.RadiusProperty, animation);

-edit-

I see now that you're using Silverlight, not wpf (where Effect implements IAnimatable)
i'll try and find a solution for silverlight

-edit2-

Have you seen/tried this? Msdn also has a sample for silverlight

-edit3-

Your direction got me towards the right answer which is as follows:

private void BlurSomething(FrameworkElement control)
        {
            var storyboard = new Storyboard();
            var animation = new DoubleAnimation
            {
                From = 0,
                To = 8,
                Duration = new TimeSpan(0, 0, 0, 1, 0),
                AutoReverse = true
            };
            var effect = new BlurEffect();
            control.Effect = effect;
            storyboard.Children.Add(animation);
            Storyboard.SetTarget(storyboard, control.Effect);
            Storyboard.SetTargetProperty(storyboard, new PropertyPath("Radius"));
            storyboard.Begin();
        }

It should be noted that Silverlight does not make animations as easy as WPF, but in the end this can be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文