0

我试图在引发事件(调用函数)时从窗口的 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

我得到的错误如下:

“值不能为空。参数名称:情节提要”

看起来它在“Button1.BeginStoryboard(StartAnimation,...)

有任何想法吗?

4

2 回答 2

1

看起来 StartAnimation 值是 Nothing 导致抛出异常。在调用 BeginStoryBoard 之前,您需要验证它是 non-Nothing。

If StartAnimation IsNot Nothing AndAlso sControl.Status = ServiceControllerStatus.Running Then
  Me.Button1.Content = "Stop"
  Button1.BeginStoryBoard(StartAnimation, HandoffBehavior.Compose)
...
于 2009-03-24T13:46:03.827 回答
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

结束类

于 2009-03-25T17:38:13.777 回答