为什么双重动画在 mainwindow.load 上不起作用? WPF VB.NET

[ad_1]

你好。
在我设计的程序中,页面随着淡入淡出效果而变化,这会改变DoubleAnimation中的OpacityProperty。

但我很惊讶为什么 DoubleAnimation 不适用于 MainWindow.Loaded ?
我的意思是在程序开始时,它不起作用。
有什么想法吗?

我应该补充一点,我将以下代码复制/粘贴到按钮下。 效果很好。 但在程序启动时,不起作用。

我尝试过的:

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

解决方案1

它不起作用,因为在 Window 类的构造函数中,您只创建了该类的实例,还没有创建窗口句柄,甚至还没有创建窗口 GDI 对象。

尝试将该淡入淡出代码移至窗口 Loaded 事件。

解决方案2

解决了!

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

[ad_2]

コメント

标题和URL已复制