Mengapa doubleanimation tidak berfungsi di mainwindow.load? Wpf VB.NET

pemrograman


Hai.
Dalam program yang saya rancang, halaman berubah dengan efek fade yang mengubah OpacityProperty di DoubleAnimation.

Tapi saya heran mengapa DoubleAnimation tidak berfungsi di MainWindow.Loaded ?
Maksudku di awal program, itu tidak berhasil.
Tolong, ada ide?

Saya harus menambahkan bahwa saya menyalin/menempelkan kode di bawah ini di bawah tombol. Itu bekerja dengan baik. Namun saat memulai program, tidak berhasil.

Apa yang saya coba:

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

Solusi 1

Ini tidak berfungsi karena di konstruktor kelas Window, Anda hanya membuat turunan kelas, Anda BELUM membuat pegangan jendela atau bahkan objek jendela GDI.

Coba pindahkan kode fade itu ke jendela Loaded event.

Solusi 2

Terselesaikan!

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をコピーしました