【发布时间】:2012-12-12 02:31:00
【问题描述】:
我正在尝试通过不使用 Storyboard 或 WPF 中其他准备好的/已经完成的东西来产生相当的效果。
我想制作平滑的效果,在某些事件(如点击)上,UI 元素会调整 2-3 秒的大小并随着颜色的变化而模糊。我想以流畅漂亮的方式制作所有这些物品。
我已经准备了这样的类来渲染我的效果的每一帧:
public static class ApplicationHelper
{
[SecurityPermissionAttribute(SecurityAction.Demand,
Flags=SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents(DispatcherPriority priority)
{
DispatcherFrame frame = new DispatcherFrame();
DispatcherOperation oper = Dispatcher.CurrentDispatcher.
BeginInvoke(priority,
new DispatcherOperationCallback(ExitFrameOperation),
frame);
Dispatcher.PushFrame(frame);
if (oper.Status != DispatcherOperationStatus.Completed)
{
oper.Abort();
}
}
private static object ExitFrameOperation(object obj)
{
((DispatcherFrame)obj).Continue = false;
return null;
}
[SecurityPermissionAttribute(SecurityAction.Demand,
Flags=SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents()
{
DoEvents(DispatcherPriority.Background);
}
}
这里我试图让它与 DispatcherTimer 一起工作:
void vb1_click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 500);
dt.Tick += new System.EventHandler(dt_Tick);
dt.Start();
}
void dt_Tick(object sender, System.EventArgs e)
{
for(int i = 0; i < 20; i++)
{
this.vb2_blur_eff.Radius = (double)i;
ApplicationHelper.DoEvents();
}
}
主要问题是,当我启动它时,我只是在等待,并且在最后时间(必须渲染最后一帧的时间),我在所有帧中都以非常快的速度获得,但以前在那里什么都不是。
如何在不使用一些现成/完成的东西的情况下,以纯 C# 方式解决它并制作完美的平滑效果?
谢谢!
【问题讨论】:
-
@punker76 它并没有重新发明它正在实施
-
好的,但是为什么不使用可视状态管理器的可视状态呢?
-
@punker76 视觉状态和状态管理器使用什么?他们不使用 DispatcherTimer 并以类似的风格工作吗?
标签: wpf silverlight effects dispatcher smooth