我如何在Silverlight 4用户控件动态动画的属性? [英] How can I animate a property dynamically in a Silverlight 4 UserControl?

查看:122
本文介绍了我如何在Silverlight 4用户控件动态动画的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了Silverlight 4的用户控件一个令人费解的限制。

I've run into a puzzling limitation in a Silverlight 4 UserControl.

我试图做到的,是有一个小组,从最小化状态滑出当按钮被pssed $ P $,但它的标题栏应可拖动与此最大化状态可以调整。

What I'm trying to achieve is to have a panel, which slides out from a minimised state when a button is pressed, but the title bar of it should be draggable with which this maximised state can be resized.

我所做的是什么滑出的动画此面板,不带硬codeD高度为面板,甚至工作的很好的父电网的maxHeight属性,但我不知道怎么能我做这个充满活力的。

What I've done for the sliding out is to animate the MaxHeight property of the parent Grid of this panel which works quite well even with no hardcoded Height for the panel, but I don't know how can I make this dynamic.

试图从$ C $一个变量绑定C-背后的'DoubleAnimation是'的'要'参数没有工作,它只是默默地被忽略。

Trying to bind a variable from the code-behind to the 'To' parameter of the 'DoubleAnimation' didn't work, it just silently gets ignored.

由于我创建用户控件重新present意见,其中x的元素:Name属性不会得到自动生成的。

As I'm creating UserControls to represent Views, the elements with x:Name properties won't get autogenerated.

我试图解决这个使用下面其中code模仿在自动生成的code会发生什么(带的布局实际上是加载后只正在做额外的奖励):

I tried to work around this using the code below which mimics what happens in the autogenerated code (with the added bonus of only being done after the layout is actually loaded):

public DoubleAnimation PanelOpenMaxHeightDoubleAnimation;

private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)

{

    var LayoutRootreference = sender as Grid;

    PanelOpenMaxHeightDoubleAnimation = ((DoubleAnimation)(LayoutRootreference.FindName("PanelOpenMaxHeightDoubleAnimation")));

    PanelOpenMaxHeightDoubleAnimation.To = 383;

}

这下尝试设置为价值的时候,作为FindName返回null但是休息(我有X:名称在XAML中手动设置这个特殊的动画PanelOpenMaxHeightDoubleAnimation)。我有偷偷怀疑FindName不能从VisualStates挑DoubleAnimations了,只有实际布局的孩子?

This however breaks when trying to set the value of To, as FindName returns null (I have x:Name manually set in XAML for this particular animation to "PanelOpenMaxHeightDoubleAnimation"). I have the sneaking suspicion FindName can't pick DoubleAnimations up from VisualStates, only actual layout children?

我在做的 http://msdn.microsoft.com/en-us/library/cc189026(v = VS.95)的.aspx#用户控件,但并没有真正理解我的选择是从本款(其他不是被非常有限的):

I did find the documentation about XAML Namescopes at http://msdn.microsoft.com/en-us/library/cc189026(v=VS.95).aspx#UserControls, but didn't really understand what my options are from this paragraph (other than being very limited):

对于用户控件的情况下,对于在定义XAML UserControl的部分没有相应的模板部件的属性约定,也没有在所有应用的模板。不过,定义和使用的名称范围保持断开连接,因为定义名称范围的定义是,当你打包成用户控件重用的组件,然后有效地密封。这里最好的做法是定义你的用户控件,使得需要被设置为修改定义XAML的任何值也暴露为用户控件的公共属性。

For the case of a UserControl, there is no equivalent template part attribute convention for parts of the UserControl in the definition XAML, nor is there a template applied at all. Nevertheless, the namescopes between definition and usage remain disconnected, because the definition namescope is defined and then effectively sealed when you package your UserControl into an assembly for reuse. A best practice here is to define your UserControl such that any value that needs to be set to modify the definition XAML is also exposed as a public property of the UserControl.

这是什么的最后一句话是什么意思?

What does it mean by the last sentence?

想知道我可以做?我应该尝试生成code的整个状态?

Wondering can I do next? Should I try to generate the entire state from code?

推荐答案

好了,设法解决它,所以我分享的解决方案。

Well, managed to work it out so I'm sharing the solution.

而不是试图去的DoubleAnimation是在资源的参考,我命名为我想动画,并使用code在原来的问题得到一个参考,布局网格:

Instead of trying to get a reference to the DoubleAnimation in Resources, I named the Grid in the layout I want to animate and get a reference to that using the code in the original question:

var SlidePanel = ((Grid)(LayoutRootreference.FindName("SlidePanel")));

这确实返回元素,并使用它可以创建一个DoubleAnimation是从无到有纯粹code一个Storyboard。我只是用这个code例如为出发点:的 http://msdn.microsoft.com/en-us/library/cc189069(VS.95)的.aspx#procedural_ code

This does return the element and using that it's possible to create a DoubleAnimation and a Storyboard from scratch purely in code. I just used this code example as a starting point: http://msdn.microsoft.com/en-us/library/cc189069(VS.95).aspx#procedural_code

最好的部分是,你可以在调用Storyboard.Begin之前每次即使在故事板设置的一切行动后更改DoubleAnimation.To参数,所以现在我在做什么只是重置了我的计算值()。

Best part is, you can change the DoubleAnimation.To parameter even after setting everything up in the Storyboard, so now what I'm doing is just resetting that to my calculated value every time before calling Storyboard.Begin().

这是一个有点繁琐手动设置所有这些,但至少它一旦你做工作得很好。

It's a bit fiddly to set all these up manually, but at least it works nicely once you do.

这篇关于我如何在Silverlight 4用户控件动态动画的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆