如何知道 ViewModel 中的动画何时完成?

发布于 2025-01-06 22:57:03 字数 149 浏览 2 评论 0原文

我正在寻找一个依赖属性,它可以告诉我动画何时结束。当值更改时,我通过设置属性“IsDirty”来显示闪烁(背景更改),让视图知道它应该为背景设置动画,但随后我需要重置下一次更改的值。

我希望你明白我的意思。

编辑:为了显示Flash,我正在使用故事板。

I'm looking for a dependence property that can tell me when an animation has ended. I'm displaying a flash (background change) when a value changes by setting a property "IsDirty" to let the View know that it should animate the background but then I need to reset the value for the next change.

I hope you get what I mean.

EDIT: To display the flash I'm using a Storyboard.

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

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

发布评论

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

评论(2

情绪失控 2025-01-13 22:57:03

您可以处理已完成< StoryBoard 的 /code> 事件并设置该时间点的任何通知。我知道您想避免代码隐藏,但我认为在这种情况下这是必需的,因为 StoryBoard 上没有这样的依赖属性,可以用来绑定数据的“IsComplete”。

You could handle the Completed event for the StoryBoard and set any notifications at that point in time. I realise you wanted to avoid code behind but I think that it will be required in this instance because there is no such dependency property on the StoryBoard for "IsComplete" with which you could data bind to.

帅的被狗咬 2025-01-13 22:57:03

你不知道,也不应该。

视图模型不应该知道视图*,更不用说视图何时完成执行动画了。 VM 拥有 IsDirty 属性绝对没问题,视图用它做什么取决于它,VM 不需要知道它。但是,您应该检查该标志的使用 - 常见用法是指示数据处于已更改且未保存的状态,当您的 UI 动画完成时,VM 仍处于脏状态,因此您不应使用此标志进行切换每次数据更改时的动画(请改用常规的 PropertyChanged 事件)。

编辑:
这是针对评论的更多澄清。

IsDirty 意味着应该通知用户有关更改的信息。

有多种方法可以通知用户数据已更改。这可以是一次性事件,例如状态区域中的图标、UI 闪烁等。IsDirty 标志只能由 VM 重置,因为 VM 知道更改的数据何时已保存。视图为您提供了一个按钮(或其他)来触发保存机制,它实际上不知道数据何时(或是否)实际保存,因此视图不应该重置该标志。您不需要视图来指示通知已完成 - 您可以在动画发生时禁用保存按钮或其他 UI 元素,以防止用户与数据交互。

如果想要每次数据变化时通知,那么实现上面提到的INotifyPropertyChanged接口,可以通过VM上的接口暴露PropertyChanged事件,然后视图可以订阅它,并且您可以针对您感兴趣的任何属性更改触发动画。您需要小心限制它的速率 - 您不希望动画因快速属性更改而变得疯狂。

*我还没有看到虚拟机了解并能够操纵或响应视图的有效用途。很多人都这样做,但正是糟糕的设计迫使他们采用这种方法。

You don't, and you shouldn't.

The viewmodel should have no clue about the view*, let alone when the view has finished executing an animation. It is absolutely fine for the VM to have an IsDirty property, what the view does with that is up to it and the VM has no need to know about it. However you should review your use of that flag - common useage is to indicate that the data is in a changed and unsaved state, when your UI animation has finished the VM is still in a dirty state so you shouldn't use this flag to toggle animations every time data changes (use a regular PropertyChanged event instead).

Edit:
here is a little more clarification in response to the comment.

The IsDirty means that the user should be notified about the change.

There can be many ways to notify the user that data has changed. This can be a one time event like an icon in a status area, a UI flash, etc. The IsDirty flag should only be reset by the VM, as the VM knows when the changed data has been saved. The view gives you a button (or whatever) to trigger the save mechanism, it doesn't actually know when (or whether) the data has actually been saved, so it follows that the view should not be resetting that flag. You don't need the view to indicate that a notification has finished - you can disable save buttons or other UI elements while the animation is occurring to prevent the user interacting with the data.

If you want to notify every time the data changes, then implement the INotifyPropertyChanged interface as mentioned above, you can expose the PropertyChanged event through the interface on the VM, then the view can subscribe to it and you can trigger the animation for whichever property changes you are interested in. You will need to be careful to rate limit it - you don't want to be going crazy with the animation in response to rapid property changes.

*I'm yet to see a valid use for the VM knowing about and being able to manipulate or respond to the view. Many people do it but it is just bad design that forces them into that approach.

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