Pourquoi la doubleanimation ne fonctionne pas sur mainwindow.load ? WPF VB.NET

la programmation


Salut.
Dans mon programme conçu, les pages changent avec un effet de fondu qui modifie l’OpacityProperty dans DoubleAnimation.

Mais je me demande pourquoi DoubleAnimation ne fonctionne pas sur MainWindow.Loaded ?
Je veux dire au démarrage du programme, ça ne marche pas.
Une idée s’il vous plaît ?

Je dois ajouter que je copie/colle le code ci-dessous sous un bouton. Cela a bien fonctionné. Mais au démarrage du programme, cela ne fonctionne pas.

Ce que j’ai essayé :

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

Solution 1

Cela ne fonctionne pas car dans le constructeur de la classe Window, vous avez uniquement créé une instance de la classe, vous n’avez PAS encore créé le handle de fenêtre ni même les objets GDI de la fenêtre.

Essayez de déplacer ce code de fondu vers l’événement Loaded de la fenêtre.

Solution 2

Résolu !

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

コメント

タイトルとURLをコピーしました