Hey all I am trying to fade in a background image on the main window.
Currently this is my code:
10 Dim storyboard__1 As New Storyboard()
20 Dim duration As New TimeSpan(0, 0, 1)
30 Dim animation As New DoubleAnimation()
50 animation.From = 0.0
60 animation.[To] = 1.0
70 animation.Duration = New Duration(duration)
90 Storyboard.SetTargetName(animation, "C:\Users\someone\Downloads\cabd.jpg")
100 Storyboard.SetTargetProperty(animation, New PropertyPath(Control.OpacityProperty))
110 storyboard__1.Children.Add(animation)
120 storyboard__1.Begin(Me.Background)
The error is on line 120 with the Me.Background.
Error BC30518 Overload resolution failed because no accessible 'Begin' can be called with these arguments:
'Public Overloads Sub Begin(containingObject As FrameworkElement)': Value of type 'Brush' cannot be converted to 'FrameworkElement'.
'Public Overloads Sub Begin(containingObject As FrameworkContentElement)': Value of type 'Brush' cannot be converted to 'FrameworkContentElement'. scrollView
What am I missing in order to call the image fade animation on the mainWindow?
You don't need to use a Storyboard. Just call BeginAnimation on the target ImageBrush:
Background.BeginAnimation(Brush.OpacityProperty, animation); // C#
As a note, Storyboard.SetTargetName uses the Name of an element (usually defined in XAML). Setting a file path like "C:\Users\someone\Downloads\cabd.jpg" is pointless.
EDIT: You should of course assign a mutable Brush instance to the Background property before trying to animate it, e.g. an ImageBrush:
var bgBrush = new ImageBrush(new BitmapImage(new Uri(#"C:\Users\someone\Downloads\cabd.jpg")));
bgBrush.BeginAnimation(Brush.OpacityProperty, animation);
Background = bgBrush;
Related
Basically I want to achieve something like this:
but I have no idea how to do it, I tried with 2 labels to combine them but the result isn't that great..
You need to draw the text from a different point of view, namely, the baseline:
Public Class MyLabel
Inherits Label
<Browsable(False)> _
Public Overrides Property AutoSize As Boolean
Get
Return False
End Get
Set(value As Boolean)
'MyBase.AutoSize = value
End Set
End Property
Protected Overrides Sub OnPaint(e As PaintEventArgs)
'MyBase.OnPaint(e)
Dim fromLine As Integer = Me.ClientSize.Height * 0.75
Dim g As Graphics = e.Graphics
Dim fontParts() As String = Me.Text.Split(".")
Using bigFont As New Font(Me.Font.FontFamily, 20)
TextRenderer.DrawText(g, fontParts(0), bigFont, _
New Point(0, fromLine - GetBaseLine(bigFont, g)), _
Me.ForeColor, Color.Empty)
If fontParts.Length > 1 Then
Dim bigWidth As Integer = TextRenderer.MeasureText(g, fontParts(0), bigFont, _
Point.Empty, TextFormatFlags.NoPadding).Width
Using smallFont As New Font(Me.Font.FontFamily, 8)
TextRenderer.DrawText(g, "." & fontParts(1), smallFont, _
New Point(bigWidth + 3, fromLine - GetBaseLine(smallFont, g)), _
Me.ForeColor, Color.Empty)
End Using
End If
End Using
End Sub
Private Function GetBaseLine(fromFont As Font, g As Graphics) As Single
Dim fontHeight As Single = fromFont.GetHeight(g)
Dim lineSpacing As Single = fromFont.FontFamily.GetLineSpacing(fromFont.Style)
Dim cellAscent As Single = fromFont.FontFamily.GetCellAscent(fromFont.Style)
Return fontHeight * cellAscent / lineSpacing
End Function
End Class
The code basically measures the height of the font from a line. In my example, I used the bottom 25% of the Label's client space to say, start drawing from this line: Me.ClientSize.Height * 0.75.
For each font you use, you would have to measure that font's baseline and subtract that from your drawing line in order to offset your drawing position of the text.
Measuring an individual character's dimensions is not easy due to aliasing and glyph overhangs. I added a small padding between the big text and the small text: bigWidth + 3 to try to make it look good. If the big number ends in a 7, the distance looks a little off because the stem of the 7 is angled.
Result:
Create a new class inherited from Label, and override the void OnPaint(PaintEventArgs e) method to change the default rendering behavior:
public class MyLabel : Label
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString("A", Font, new SolidBrush(ForeColor), 10, 10);
e.Graphics.DrawString("B", new Font(Font.FontFamily, 20), new SolidBrush(ForeColor), 50, 10);
}
}
As a result, "B" will be two times larger the "A". You can achieve your goal in the same way, but you have to calculate the position of your sub-strings ("145", ".", "54") and draw them.
Use devexpress LabelControl.AllowHtmlString property to true and use the supported <size> tag within the LabelControl's Text property as detailed in the HTML Text Formatting documentation.
you can use user control WPF in windows form. to do that do this step.
1. add user control to the windows form
2.from xml of usercontrol name grid like t1
3. add this function to the usercontrol.wpf.cs
public void Actor(string text)
{
StringBuilder sb = new StringBuilder();
sb.Append(#"<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> ");
sb.Append(text);
sb.Append(#"</TextBlock>");
TextBlock myButton = (TextBlock)XamlReader.Parse(sb.ToString());
this.t1.Children.Clear();
t1.Children.Add(myButton);
}
4. after that from form1.css add this function in every where you want.
userControl11.Actor("<Run Text='Hi ' FontWeight='Bold'/><Run Text='Hitler ' FontWeight='Bold'/>");
userControl11.Actor(" < Run FontWeight = 'Bold' FontSize = '14' Text = 'This is WPF TextBlock Example. ' />");
you can mange the write code "" of Actor function by using xml wpf.
I used to animate my border with a from to animation but now I wanted to add a thicknesskeyframe to make it look better. I'm using .net 4.0 and vb.
Dim t As New ThicknessAnimationUsingKeyFrames()
t.RepeatBehavior = RepeatBehavior.Forever
Dim instance As ThicknessKeyFrame
instance.Value = New Thickness(0, 0, 0, 0)
instance.KeyTime = TimeSpan.FromSeconds(0.0)
t.KeyFrames.Add(instance)
Dim instance2 As ThicknessKeyFrame
instance2.Value = New Thickness(1, 1, 1, 1)
instance2.KeyTime = TimeSpan.FromSeconds(0.5)
t.KeyFrames.Add(instance)
I can't instantiate the thickness keyframe I think it's an interface but I don't know how to use it in this context.
fistly, you need to instantiate your instances
Dim instance As New DiscreteThicknessKeyFrame()
the bottom line is incorrect:
t.KeyFrames.Add(instance)
it should be
t.KeyFrames.Add(instance2)
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>
friends,thank you for your time!
There's a problem in the codes below in WPF.I use a ProgressBar and a Animation which show the progress of value changes.When the Animation finished,I try to reset the value to 0 ,so that the ProgressBar can begin a new Animation from 0 to 100 again.But the result is that I can't set the value to 0,it seems that it would be 100 forever no matter how I tried!!!!!
Could you please give me some ideas,thank you!
pbStatus.Value = 0;//Promblem!! pbStatus is a ProgressBar
Duration dr = new Duration(TimeSpan.FromSeconds(2));
DoubleAnimation da = new DoubleAnimation(100, dr);
pbStatus.IsIndeterminate = false;
pbStatus.Visibility = Visibility.Visible;
pbStatus.BeginAnimation(ProgressBar.ValueProperty, da);
Please see this article.
Summary: There are three ways to set a value after an animation.
Set the animation's FillBehavior property to Stop
Remove the entire Storyboard. (Not applicable since you have no storyboard)
Remove the animation from the individual property.
(1) Set the fill behaviour of the animation to stop:
da.FillBehavior = FillBehavior.Stop;
(3) Remove the animation by calling this before setting the new value:
pbStatus.BeginAnimation(ProgressBar.ValueProperty, null);
From this article:
private void CreateDynamicProgressBarControl()
{
ProgressBar PBar2 = new ProgressBar();
PBar2.IsIndeterminate = false;
PBar2.Orientation = Orientation.Horizontal;
PBar2.Width = 100;
PBar2.Height = 10;
Duration duration = new Duration(TimeSpan.FromSeconds(10));
DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
PBar2.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
SBar.Items.Add(PBar2);
}
I'm doing a Surface Application.
And there I have something like a bulletin board where little cards with news on it are pinned on.
On click they shall fly out of the board and scale bigger.
My storyboard works well, except for the first time it runs. It's not a smooth animation then but it scales to its final size immediately and it's the same with the orientation-property. Just the center-property seems to behave correctly.
This is an example for one of my Storyboards doing that:
Storyboard stb = new Storyboard();
PointAnimation moveCenter = new PointAnimation();
DoubleAnimationUsingKeyFrames changeWidth = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames changeHeight = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames changeOrientation = new DoubleAnimationUsingKeyFrames();
moveCenter.From = News1.ActualCenter;
moveCenter.To = new Point(250, 400);
moveCenter.Duration = new Duration(TimeSpan.FromSeconds(1.0));
moveCenter.FillBehavior = FillBehavior.Stop;
stb.Children.Add(moveCenter);
Storyboard.SetTarget(moveCenter, News1);
Storyboard.SetTargetProperty(moveCenter, new PropertyPath(ScatterViewItem.CenterProperty));
changeWidth.Duration = TimeSpan.FromSeconds(1);
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeWidth.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeWidth);
Storyboard.SetTarget(changeWidth, News1);
Storyboard.SetTargetProperty(changeWidth, new PropertyPath(FrameworkElement.WidthProperty));
changeHeight.Duration = TimeSpan.FromSeconds(1);
changeHeight.KeyFrames.Add(new EasingDoubleKeyFrame(400, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeHeight.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeHeight);
Storyboard.SetTarget(changeHeight, News1);
Storyboard.SetTargetProperty(changeHeight, new PropertyPath(FrameworkElement.HeightProperty));
changeOrientation.Duration = TimeSpan.FromSeconds(1);
changeOrientation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(new System.TimeSpan(0, 0, 1))));
changeOrientation.FillBehavior = FillBehavior.Stop;
stb.Children.Add(changeOrientation);
Storyboard.SetTarget(changeOrientation, News1);
Storyboard.SetTargetProperty(changeOrientation, new PropertyPath(ScatterViewItem.OrientationProperty));
stb.Begin(this);
News1.Center = new Point(250, 400);
News1.Orientation = 0;
News1.Width = 266;
News1.Height = 400;
Pin1.Visibility = Visibility.Collapsed;
news1IsOutside = true;
Scroll1.IsEnabled = true;
What's wrong with it?
The Problem
The essence of the problem is that you are calling stb.Begin() and then immediately changing News1.Width, etc. Unfortunately stb.Begin() is not guaranteed to start the animations immediately: At times it does so in a dispatcher callback.
What is happening to you is that the first time your storyboard executes, stb.Begin() schedules a dispatcher callback to start the animations and immediately returns. The next four lines of your code update the values:
News1.Center = new Point(250, 400);
News1.Orientation = 0;
News1.Width = 266;
News1.Height = 400;
When the animations actually start in the dispatcher callback they see the new values and use those as their starting values. This causes the object appears to jump to the new value immediately.
For example, changeWidth declares a keyframe that animates the value to 266:
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, ...
And later the initial width is set to 266:
News1.Width = 266;
So if the storyboard is delayed starting, the width will animate from 266 to 266. In other words, it will not change. If you later use another animation to change News1.Width to something other than 266 and then run the changeWidth animation, it will work.
Your moveCenter animation works reliably because it actually sets its From value to the current value:
moveCenter.From = News1.ActualCenter;
moveCenter.To = new Point(250, 400);
Thus the animation always starts at the old center, even if the News1.Center = new Point(250,400) once the animation has started.
The Solution
Just as you set "From" on your PointAnimation, you can also set an initial value in your other animations. This is done by adding a key frame at time=0 specifying the current width:
changeWidth.Duration = TimeSpan.FromSeconds(1);
changeWidth.KeyFrames.Add(new DiscreteDoubleKeyframe(News1.Width, KeyTime.Paced));
changeWidth.KeyFrames.Add(new EasingDoubleKeyFrame(266, KeyTime.Paced));
This code uses the fact that KeyTime.Paced automatically results in 0% and 100% if there are two key frames. In fact, setting the first frame as KeyFrame.Paced will always be equivalent to KeyTime.FromTimeSpan(TimeSpan.Zero).
Another solution might be to use FillBehavior.HoldEnd, then hook up to the Storyboard.Completed event and:
Set the local values as you originally did
Call Storyboard.Remove
This would also have the advantage that the local values will be accessible from the properties.