我试着写一个简单的(独立的)C#扩展方法做淡入一个通用的WPF的UIElement的,但解决方案(和code样品),我发现都含有大量的运动部件(比如设置了一个故事,等...)
有关参考这里是API的方法,我想创建类型的一个例子。这code将根据提供的值旋转的UIElement的(fromValue,toValue,持续时间和环路)
的公共静态牛逼旋转< T>(这件T的UIElement,双fromValue,双toValue,诠释durationInSeconds,布尔loopAnimation) 其中T:的UIElement { 返回(T)uiElement.wpfInvoke( ()=> { DoubleAnimation是DoubleAnimation是=新DoubleAnimation是(fromValue,toValue,新的持续时间(TimeSpan.FromSeconds(durationInSeconds))); RotateTransform rotateTransform =新RotateTransform(); uiElement.RenderTransform = rotateTransform; uiElement.RenderTransformOrigin =新System.Windows.Point(0.5,0.5); 如果(loopAnimation) doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; rotateTransform.BeginAnimation(RotateTransform.AngleProperty,DoubleAnimation是); 返回的UIElement; }); }
解决方案
听起来你正在寻找这样的事情:
公共静态牛逼淡入< T>(这件T的UIElement,诠释durationInSeconds)
{
返回uiElement.FadeFromTo(0,1,durationInSeconds,假);
}
公共静态牛逼淡出< T>(这件T的UIElement,诠释durationInSeconds)
{
返回uiElement.FadeFromTo(1,0,durationInSeconds,假);
}
公共静态牛逼FadeFromTo< T>(这件T的UIElement,
双fromOpacity,双toOpacity,
INT durationInSeconds,布尔loopAnimation)
其中T:的UIElement
{
返回(T)uiElement.wpfInvoke(()=>
{
VAR DoubleAnimation是=
新DoubleAnimation是(fromOpacity,toOpacity,
新持续时间(TimeSpan.FromSeconds(durationInSeconds)));
如果(loopAnimation)
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
uiElement.BeginAnimation(UIElement.OpacityProperty,DoubleAnimation是);
返回的UIElement;
});
}
I'm trying to write a simple (stand alone) C# extension method to do a Fade-In of a generic WPF UIElement, but the solutions (and code samples) that I found all contain a large number of moving parts (like setting up a story, etc...)
For reference here is an example of the type of API method I would like to create. This code will rotate an UIElement according to the values provided (fromValue, toValue, duration and loop)
public static T rotate<T>(this T uiElement, double fromValue, double toValue, int durationInSeconds, bool loopAnimation) where T : UIElement { return (T)uiElement.wpfInvoke( ()=>{ DoubleAnimation doubleAnimation = new DoubleAnimation(fromValue, toValue, new Duration(TimeSpan.FromSeconds(durationInSeconds))); RotateTransform rotateTransform = new RotateTransform(); uiElement.RenderTransform = rotateTransform; uiElement.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5); if (loopAnimation) doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; rotateTransform.BeginAnimation(RotateTransform.AngleProperty, doubleAnimation); return uiElement; }); }
解决方案
Sounds like you're looking for something like this:
public static T FadeIn<T>(this T uiElement, int durationInSeconds)
{
return uiElement.FadeFromTo(0, 1, durationInSeconds, false);
}
public static T FadeOut<T>(this T uiElement, int durationInSeconds)
{
return uiElement.FadeFromTo(1, 0, durationInSeconds, false);
}
public static T FadeFromTo<T>(this T uiElement,
double fromOpacity, double toOpacity,
int durationInSeconds, bool loopAnimation)
where T : UIElement
{
return (T)uiElement.wpfInvoke(()=>
{
var doubleAnimation =
new DoubleAnimation(fromOpacity, toOpacity,
new Duration(TimeSpan.FromSeconds(durationInSeconds)));
if(loopAnimation)
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
uiElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);
return uiElement;
});
}