My code is here, and I can't edit the offset property of TranslateTransform3D any more after running this:
DoubleAnimation doubleAnimationX =
new DoubleAnimation(x,
new Duration(TimeSpan.FromSeconds(second)));
DoubleAnimation doubleAnimationY =
new DoubleAnimation(y,
new Duration(TimeSpan.FromSeconds(second)));
DoubleAnimation doubleAnimationZ =
new DoubleAnimation(z,
new Duration(TimeSpan.FromSeconds(second)));
translate.BeginAnimation(TranslateTransform3D.OffsetXProperty, doubleAnimationX);
translate.BeginAnimation(TranslateTransform3D.OffsetYProperty, doubleAnimationY);
translate.BeginAnimation(TranslateTransform3D.OffsetZProperty, doubleAnimationZ);
TranslateTransform3D derives from Freezable. Your translate is being frozen after the animation, which makes it immutable.
Related
I have two canvases and a textblock with name tbmarquee, on window load it animates from top to bottom, but i want to stop when i over the mose on it, and when i click it goes to the link, how to do it ?
void Window1_Loaded(object sender, RoutedEventArgs e)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = -tbmarquee.ActualHeight;
doubleAnimation.To = canMain.ActualHeight;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
tbmarquee.BeginAnimation(Canvas.TopProperty, doubleAnimation);
}
On mouse over event handler of tbmarquee, call BeginAnimation() again with second argument set to null to stop the animation :
tbmarquee.BeginAnimation(Canvas.TopProperty, null);
Related question : How to stop an animation in C# / WPF?
I need to convert byte[] to BitmapImage and show it in WPF image control. (img.Source = ...).
if i convert it like this:
m_photo = new BitmapImage();
using (MemoryStream stream = new MemoryStream(photo.ToArray()))
{
m_photo.BeginInit();
m_photo.StreamSource = stream;
m_photo.EndInit();
}
it can't do XAML binding to Source property because "m_photo owns another stream"... What can I do?
Set the cache option to OnLoad after begininit
m_photo.CacheOption = BitmapCacheOption.OnLoad;
EDIT: complete code for bmp array to Image source
DrawingGroup dGroup = new DrawingGroup();
using (DrawingContext drawingContext = dGroup.Open())
{
var bmpImage = new BitmapImage();
bmpImage.BeginInit();
bmpImage.CacheOption = BitmapCacheOption.OnLoad;
bmpImage.StreamSource = new MemoryStream(photoArray);
bmpImage.EndInit();
drawingContext.DrawImage(bmpImage, new Rect(0, 0, bmpImage.PixelWidth, bmpImage.PixelHeight));
drawingContext.Close();
}
DrawingImage dImage = new DrawingImage(dGroup);
if (dImage.CanFreeze)
dImage.Freeze();
imageControl.Source = dImage;
Ok, I just found solution. If use this code (converting byte[] to bitmapSource) in code of class - you have this error, that the object is in another stream. But if create a Converter (IValueConverter) and use it with same code of converting in XAML binding - everything ok!
Thanks everybody!
I'm new at Silverlight and trying samples for things animating the opacity of an object programatically.
I've come up with the following code:
MapShape myTestShape= RadMapInformationLayer.Items[0] as MapShape;
SolidColorBrush brush = new SolidColorBrush();
brush.Color = Colors.Purple;
myTestShape.Fill = brush;
//create a duration object
Duration duration = new Duration(TimeSpan.FromSeconds(5));
//create the storyboard
Storyboard story = new Storyboard();
//create double animation
DoubleAnimation animation = new DoubleAnimation();
//set the duration property
animation.Duration = duration;
//set the from and too values
animation.From = 1.0;
animation.To = 0.0;
//add the duration to the storyboard
story.Duration = duration;
//now set the target of the animation
Storyboard.SetTarget(animation, myTestShape);
//set the target property of this object
Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
//add the double animations to the story board
story.Children.Add(animation);
if (!LayoutRoot.Resources.Contains("story1"))
LayoutRoot.Resources.Add("story1", story);
story.Begin();
For the property path I've also tried:
1. new PropertyPath("(FrameworkElement.Opacity)")
2. new PropertyPath("(FrameworkElement.Opacity)")
3. new PropertyPath("(Control.Opacity)")
And a few others, I'm having zero luck with this.
Can anyone see where I'm going wrong?
Thanks,
Jacques
I have done databinding to MapShape.FillProperty before.
Try:
//create double animation
ColorAnimation animation = new ColorAnimation();
//set the duration property
animation.Duration = duration;
//set the from and too values
animation.From = Colors.Purple;
animation.To = Colors.Transparent;
//add the duration to the storyboard
story.Duration = duration;
//now set the target of the animation
Storyboard.SetTarget(animation, rect);
//set the target property of this object
Storyboard.SetTargetProperty(animation, new PropertyPath("(MapShape.Fill).Color"));
EDIT:
As for why your code is not working -- Looking through MapShape.SetShapeFillStroke(), it appears that Telerik won't bind the MapShape's Opacity to its inner primitive shape's Opacity unless you provide a fill. Do you have a Fill defined in XAML? If not, try providing one. Otherwise, maybe the code is defined too early in the Shape's lifecycle (in or after ReadCompleted)?
<telerik:InformationLayer x:Name="StateLayer">
<telerik:InformationLayer.Reader>
...
</telerik:InformationLayer.Reader>
<telerik:InformationLayer.ShapeFill>
<telerik:MapShapeFill Fill="{StaticResource CommonBackgroundLightBrush}" Stroke="#5A636B" StrokeThickness="1" />
</telerik:InformationLayer.ShapeFill>
i am trying to rotate an object from code behind. Code below:
Storyboard storyBoard = new Storyboard();
//Transform
RotateTransform rotate = new RotateTransform();
rotate.Angle = 45;
rotate.CenterX = 50;
rotate.CenterY = 20;
RodBorder.RenderTransform = rotate;
DoubleAnimation Anim = new DoubleAnimation();
Anim.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
Anim.SetValue(Storyboard.TargetPropertyProperty, rotate);
Storyboard.SetTargetProperty(Anim, new PropertyPath("RenderTransform.Angle"));
Storyboard.SetTarget(Anim, RodBorder);
storyBoard.Children.Add(Anim);
storyBoard.Begin();
RodBorder is a Border which i want to rotate. The problem here is probably the PropertyPath, because i got an exception there.(System.Windows.PropertyPath cannot be set to type System.Windows.Media.RotateTransform) Any ideas are welcome, thank you for your help.
Try Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Rotation)".
You can always verify syntax by building sample animation in Blend.
I used this code to animation my window:
winLogin login = new winLogin();
login.Owner = this;
login.Show();
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = this.Left + ((this.Width - login.Width) / 2);
da.AutoReverse = false;
da.Duration = new Duration(TimeSpan.FromSeconds(0.1));
login.BeginAnimation(Window.LeftProperty, da);
Problem is that whenever i set the Left property of this window(after the animation), it goes crazy.
I used this code to align the child windows to be always on the center but the Left property of the windows on which i used an animation cannot be properly changed.
private void Window_LocationChanged(object sender, EventArgs e)
{
foreach (Window win in this.OwnedWindows)
{
win.Top = this.Top + ((this.Height - win.Height) / 2);
win.Left = this.Left + ((this.Width - win.Width) / 2);
}
}
First of all, when you set an animation you should always remove the potential previous animation of that property:
login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);
If you don't so this you will get a memory leak and probably some other undesired behavior.
Also due to the DependencyProperty precedence you can not set a value on a DependecyProperty that has an active animation, wich is the case in your animation because its FillBehavior is set to HoldEnd (the default). Again you would have to remove the animation first.