我正在尝试在引发事件(调用函数)时从窗口的vb代码触发窗口的XAML文件中声明的动画,就像窗口的“已加载”事件一样。
以下是我如何宣布动画(作为故事板):
Dim StartAnimation As Storyboard = DirectCast(FindName("ServiceOn"), Storyboard)
Dim StopAnimation As Storyboard = DirectCast(FindName("ServiceOff"), Storyboard)
这是失败函数的代码:
Public Function CheckStatus() As Boolean
If sControl.Status = ServiceControllerStatus.Running Then
Me.Button1.Content = "Stop"
Button1.BeginStoryboard(StartAnimation, HandoffBehavior.Compose, isControllable:=False)
ElseIf sControl.Status = ServiceControllerStatus.Stopped Then
Me.Button1.Content = "Start"
Button1.BeginStoryboard(StopAnimation, HandoffBehavior.Compose, isControllable:=False)
End If
End Function
我得到的错误如下:
“值不能为空。参数名称:storyboard”
看起来它在“Button1.BeginStoryboard(StartAnimation,...)
之后遗漏了什么有什么想法吗?
答案 0 :(得分:1)
看起来StartAnimation值是Nothing导致抛出Exception。在调用BeginStoryBoard之前,您需要验证这是非Nothing。
If StartAnimation IsNot Nothing AndAlso sControl.Status = ServiceControllerStatus.Running Then
Me.Button1.Content = "Stop"
Button1.BeginStoryBoard(StartAnimation, HandoffBehavior.Compose)
...
答案 1 :(得分:0)
我实际上发现了问题所在:
当我宣布动画时,我是在初始化级别进行的,而不是在事件被调整时,以致新类实际上是= Null。
诀窍是将其粘贴到逻辑代码而不是声明部分,以使其工作。这是最终的代码(它工作得很好):
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.BackgroundWorker
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.ServiceProcess
Partial Public Class Window1
Public Sub New()
MyBase.New()
Me.InitializeComponent()
End Sub
Private WithEvents worker As New BackgroundWorker
Dim sControl As New ServiceController("Spooler")
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
worker.WorkerReportsProgress = True
CheckStatus()
End Sub
Public Function CheckStatus() As Boolean
If sControl.Status = ServiceControllerStatus.Running Then
Dim StartAnimation As Storyboard = DirectCast(FindResource("ServiceIsStarted"), Storyboard)
Me.Button1.Content = "Stop"
Me.BeginStoryboard(StartAnimation)
ElseIf sControl.Status = ServiceControllerStatus.Stopped Then
Dim StopAnimation As Storyboard = DirectCast(FindResource("ServiceIsStopped"), Storyboard)
Me.Button1.Content = "Start"
Me.BeginStoryboard(StopAnimation)
End If
End Function
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
If sControl.Status = ServiceControllerStatus.Running Then
sControl.Stop()
sControl.Refresh()
worker.ReportProgress(100)
ElseIf sControl.Status = ServiceControllerStatus.Stopped Then
sControl.Start()
sControl.Refresh()
worker.ReportProgress(100)
End If
End Sub
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles worker.ProgressChanged
If sControl.Status = ServiceControllerStatus.Running Then
Dim StartAnimation As Storyboard = DirectCast(FindResource("ServiceIsStarted"), Storyboard)
Me.Button1.Content = "Stop"
Me.BeginStoryboard(StartAnimation)
ElseIf sControl.Status = ServiceControllerStatus.Stopped Then
Dim StopAnimation As Storyboard = DirectCast(FindResource("ServiceIsStopped"), Storyboard)
Me.Button1.Content = "Start"
Me.BeginStoryboard(StopAnimation)
End If
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
worker.RunWorkerAsync()
End Sub
结束班