Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.
In my designed program, pages change with fade effect which changes the OpacityProperty in DoubleAnimation.

But I surprised why DoubleAnimation doesn't work on MainWindow.Loaded ?
I mean at the starting of program, it doesn't work.
Any Idea please?

I should add that I copy/paste the below code under a button. It worked fine. But on starting of program, doesn't work.

What I have tried:

XML
Private Sub New()
        InitializeComponent()

        'Fade in MainWindow page.
        Dim DAGrid = New DoubleAnimation With {
            .From = 0.0,
            .To = 1.0,
            .FillBehavior = FillBehavior.Stop,
            .BeginTime = TimeSpan.FromSeconds(0),
            .Duration = New Duration(TimeSpan.FromSeconds(3))
        }
        Dim storyboard = New Storyboard()
        Storyboard.SetTarget(DAGrid, MainWindow)
        Storyboard.SetTargetProperty(DAGrid, New PropertyPath(OpacityProperty))
        storyboard.Children.Add(DAGrid)
        storyboard.Begin()
    End Sub
Posted

It doesn't work because in the constructor of the Window class, you have only created an instance of the class, you have NOT created the window handle or even the window GDI objects yet.

Try moving that fade code to the window Loaded event.
 
Share this answer
 
Comments
Sh.H. 10-Dec-23 15:34pm    
I just changed the first line with this:
Private Sub MainWindow_Loaded(sender as Object, e as EventArgs) Handles MainWindow.Loaded


But ... No luck!
Could you please take a look at this?
Dave Kreskowiak 10-Dec-23 21:51pm    
I currently don't have time to sit down with this. Google for "wpf fade window in" and you should come up with plenty of examples.
Solved!

VB
Sub New()
    InitializeComponent()

    TheMainWindow.Opacity = 0
End Sub

Private Sub TheMainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles TheMainWindow.Loaded
    'Fade in MainWindow page.
    Dim DATheMainWindow = New DoubleAnimation With {
        .From = 0.0,
        .To = 1.0,
        .FillBehavior = FillBehavior.Stop,
        .BeginTime = TimeSpan.FromSeconds(0),
        .Duration = New Duration(TimeSpan.FromSeconds(1))
    }
    AddHandler DATheMainWindow.Completed, Sub(a, b) TheMainWindow.Opacity = 1
'TheMainWindow is the Name Property of MainWindow
'a and b is just a temporary variables and no need to declare.
    Dim storyboardTheMainWindow = New Storyboard()
    Storyboard.SetTarget(DATheMainWindow, TheMainWindow)
    Storyboard.SetTargetProperty(DATheMainWindow, New PropertyPath(OpacityProperty))
    storyboardTheMainWindow.Children.Add(DATheMainWindow)
    storyboardTheMainWindow.Begin()
End Sub
 
Share this answer
 
v2
Comments
Chris Copeland 11-Dec-23 4:47am    
So you took Dave Kreskowiak's answer, applied it to your project, and then posted it as your own solution and accepted it? That's not very nice. You should give credit where credit is due.
Sh.H. 12-Dec-23 3:19am    
What do you mean?
His answer was not helpful!
Would you please let me know why are you saying this? And why you say I took his answer?
Thanks
Chris Copeland 12-Dec-23 11:33am    
He told you to use the window Loaded event for the animation, so you did, then posted your code and accepted it as the solution. You did exactly what Dave told you to do, but you've chosen not to accept his answer as the solution in favour of your own.

Just because he didn't give you the exact code doesn't mean he didn't provide the answer, he pointed you in the right direction and you consequently found your answer, so his answer should have been accepted.
Sh.H. 13-Dec-23 2:00am    
You are right.
I'm sorry about that.
Thank you and him to pointing me in right direction.
And thank you because telling this to me.
I clicked "Accept Solution" on his answer.
Andre Oosthuizen 11-Dec-23 13:54pm    
Agreed 100% !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900