Tại sao doubleanimation không hoạt động trên mainwindow.load? WPF VB.NET

lập trình


CHÀO.
Trong chương trình được thiết kế của tôi, các trang thay đổi với hiệu ứng mờ dần làm thay đổi OpacityProperty trong DoubleAnimation.

Nhưng tôi ngạc nhiên tại sao DoubleAnimation không hoạt động trên MainWindow.Loaded?
Ý tôi là khi bắt đầu chương trình, nó không hoạt động.
Bất kỳ ý tưởng nào xin vui lòng?

Tôi nên thêm rằng tôi sao chép/dán mã bên dưới vào một nút. Nó hoạt động tốt. Nhưng khi bắt đầu chương trình, nó không hoạt động.

Những gì tôi đã thử:

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

Giải pháp 1

Nó không hoạt động vì trong hàm tạo của lớp Window, bạn chỉ tạo một thể hiện của lớp, bạn CHƯA tạo bộ điều khiển cửa sổ hoặc thậm chí các đối tượng GDI của cửa sổ.

Hãy thử di chuyển mã mờ dần đó sang sự kiện Đã tải của cửa sổ.

Giải pháp 2

Đã giải quyết!

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