动态动画、命令和关注点分离

发布于 2024-08-07 00:32:14 字数 259 浏览 4 评论 0原文

场景:我有一个(数字)文本框、一个按钮和一个标签。单击按钮时,我希望标签对文本框中的数值进行“动画”处理(如旋转拨号盘)

假设:

a)故事板中的动画不能具有数据绑定(因为它们不是 FrameworkElements) b) Silverlight 中缺乏触发器

什么是最好的、视图模型与视图故事板耦合最少的方式来更新目标动画值并在单击按钮时启动动画?

注意:该场景是概念性的,因此不要专注于“动画”数字或任何内容的细节

Scenario: I have a (numeric) textbox, a button, and a label. When the button is clicked I'd like the label to "animate" to the numeric value in the textbox (like a spinning dial)

Given:

a) that animations in storyboards cannot have databindings (because they are not FrameworkElements)
b) the lack of triggers in Silverlight

What is the best, and with least coupling of the view model to the view's storyboard, way to update the target animation value and start the animation when the button is clicked?

Note: The scenario is conceptual, so don't concentrate on the specifics of 'animating' numbers or anything

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

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

发布评论

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

评论(2

绾颜 2024-08-14 00:32:14

如果您的目标严格是减少视图中的代码隐藏,我认为标签上的附加行为可以达到此目的。标签上附加的行为将公开要动画化的数字,当该数字更改时,将运行动画(以代码形式)以将旧值动画化为新值。

一个缺点是您的动画现在是在代码中,除非您将其模板化(仅具有假值开始)版本存储在资源文件中,您可以在其中根据需要加载它并替换模板化值。

这篇乔什·史密斯的文章似乎是依附行为的权威;

http://joshsmithonwpf.wordpress.com/2008/08 /30/依恋行为简介/

If your goal is strictly to reduce the code-behind in the view I think that an attached behaviour on the Label would work for this. The attached behaviour on the label would expose the number to be animated to and when this number changes an animation (in code) would be run to animate from the old value to the new value.

One drawback is that your animation is now in code, unless you store a templated (just has fake values to start with) version of it in a resource file somewhere where you can load it as needed and replace the templated values.

This article from Josh Smith seems to be the authority on Attached Behaviours;

http://joshsmithonwpf.wordpress.com/2008/08/30/introduction-to-attached-behaviors/

云仙小弟 2024-08-14 00:32:14

我最近必须在 MVVM 应用程序中解决类似的问题。我的问题是我需要将容器的高度从零设置为自动。由于“自动”是一个动态值,我认识到动画(或故事板)需要按需构建(或操作)。我实施的解决方案涉及使用视图隐藏代码来更新和触发动画。

这不是 MVVM 最友好的方法;但是,WPF 中的动画在 XAML 中可能会很棘手。由于此解决方案实际上只是 XAML 限制的解决方法,因此将代码直接绑定到视图似乎可以。同样,如果视图被模拟,那么就不会有框架元素进行动画处理,因此将此代码放在虚拟机端确实没有意义。

有人有更好的方法吗?

I recently had to solve a similar problem in an MVVM application. My problem was that I needed to animate a container's height from zero to auto. Since Auto is a dynamic value I recognized that the animation (or storyboard) would need to be built (or manipulated) on demand. The solution that I put in place involved using view code-behind to update and fire the animation.

This isn't the most MVVM-friendly approach; however, animations in WPF can be tricky in XAML. Since this solution is really just a workaround for a XAML limitation it seems okay to tie the code directly to the view. Likewise, if the views were mocked then there would be no framework elements to animate, so it really wouldn't make sense to place this code on the VM side.

Does anybody have a better approach?

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