在银光中淡入淡出一条路径?
我有一个这样定义的路径:
<Viewbox Visibility="Collapsed" x:Name="Tick" Grid.Column="1" Grid.Row="1">
<Canvas MaxWidth="100" MaxHeight="100" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="100" Height="100" Canvas.Left="0" Canvas.Top="0">
<Path ...Fill="#FF00B800" x:Name="MyPath" (path dimensions, lines, etc) .../>
</Canvas>
</Viewbox>
现在我想做的是操纵填充,这样它将导致路径具有淡入/淡出效果。基本上,根据路径所在的视图框是否可见,使填充 alpha 组件向不透明或透明方向移动。因此,当可见时,路径会淡入,当折叠时,路径会淡出。
I have a path defined as such:
<Viewbox Visibility="Collapsed" x:Name="Tick" Grid.Column="1" Grid.Row="1">
<Canvas MaxWidth="100" MaxHeight="100" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="100" Height="100" Canvas.Left="0" Canvas.Top="0">
<Path ...Fill="#FF00B800" x:Name="MyPath" (path dimensions, lines, etc) .../>
</Canvas>
</Viewbox>
Now what I'd like to do is manipulate fill such that it will cause the path to have a fade in/fade out effect. Basically make fills alpha component either move towards opaque or transparent based on whether or not the viewbox the path is inside is Visible. So when visible the path fades in, when collapsed the path fades out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要达到的效果是非常好的,但是你当前的计划有一个严重的问题。当更高级别元素(本例中为
Viewbox
)的Visibility
设置为Visibility.Collapsed
时,该元素和所有子元素立即不再可见。您正是在此时希望Path
开始淡出。因此,
Path
已经不可见,并且启动动画来逐渐降低其不透明度不会有任何好处,因为它已经消失了。换句话说,当可见性设置为 Visibity.Collapsed 时,对元素内部的内容执行任何有用的操作都为时已晚,因为用户将看不到它们。如果可以的话,您会希望预见未来并知道您将更改可见性并启动动画,以便它在您“关闭元素的窗帘”之前完成。当我们使元素可见时,同样的问题并不适用,因为一切都很完美:我们变得可见并开始淡入动画。但既然有一半的效果就不起作用了,我们还有一个大问题。
这个问题的解决方案是向上移动一个级别,看看我们正在尝试做什么。在 XAML 中,我们只有被动元素:
Viewbox
、Canvas
和Path
。但也许它们的作用更像控件或辅助控件,例如检查CheckBox
或类似复选框的控件。控件可以有状态:
,并且这些状态可以具有过渡效果,这要归功于
VisualStateManager
。因此,如果淡入和淡出效果是控制行为的一部分,那么我们就有了一整套复杂而强大的工具集来解决我们的问题。不知道你的情况是不是这样。
即使情况并非如此,在 Silverlight 上实现过渡效果的一种非常可行的方法是将元素转换为无外观的控件,这仅仅是为了利用 VisualStateManager,因为它使事情变得如此简单。也许这个替代方案适合您的情况。
The effect you are trying achieve is a classy one but there is a serious problem with your current plan. When the
Visibility
of a higher-level element,Viewbox
in this case, is set toVisibility.Collapsed
, the element and all sub-elements are immediately no longer visisble. It is at this point that you want the fade-out of thePath
to begin.So the
Path
is already not visible and starting an animation to gradually reduce its opacity will no do any good because it is already gone. In other words, by the time the visibility is set toVisiblity.Collapsed
, it is too late to do anything useful with things inside the element because the user won't see them. If you could, you would want to see into the future and know that you are going to change the visibility and start an animation so that it finishes before you "close the curtain" on the element.The same problem doesn't apply to when we make the element visible because everything is perfect: we become visible and start the fade-in animation. But since half of the effect is not going to work, we still have a big problem.
The solution to this problem is move up a level and see what we're trying to do. In the XAML we only have passive elements,
Viewbox
,Canvas
, andPath
. But maybe these are acting more like controls or assisting controls, for example being the check for aCheckBox
or a checkbox-like control.A control can have states:
and those states can have transition effects, thanks to the
VisualStateManager
.So if the fade-in and fade-out effects are part of control behavior, then we have a whole sophisticated powerful toolset available to solve our problem. I don't know if this is the case in your situation.
Even if it is not the case, a very workable approach to transition effects on Silverlight is to transform your elements into a lookless control, solely for the purpose of utilizing the
VisualStateManager
, because it makes things so easy. Perhaps this alternative can work in your situation.