TuxMeister
TuxMeister

Reputation: 275

WPF starting animation from vb.net code giving error

I'm trying to trigger an animation declared in the window's XAML file from the window's vb code when an event is raised (calling a function), like a window's "loaded" event.

Here's how I declare the animation (as a storyboard):

Dim StartAnimation As Storyboard = DirectCast(FindName("ServiceOn"), Storyboard)
Dim StopAnimation As Storyboard = DirectCast(FindName("ServiceOff"), Storyboard)

And here's the code for the function that is failing:

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

The error that I'm getting is the following:

"Value cannot be null. Parameter name: storyboard"

It looks like it's missing something right after "Button1.BeginStoryboard(StartAnimation,...)

Any ideas?

Upvotes: 0

Views: 2526

Answers (2)

TuxMeister
TuxMeister

Reputation: 275

I actually figured out what the problem was:

When I declared the animation I did it at initializing level, not when the event was raized so that new class was actually = Null.

The trick is to stick it into the logic code instead of the declaration part in order for it to work. This is the final code (it workes just great):

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

End Class

Upvotes: 0

JaredPar
JaredPar

Reputation: 754545

It looks like the StartAnimation value is Nothing which is causing the Exception to be thrown. You need to verify this is non-Nothing before calling BeginStoryBoard.

If StartAnimation IsNot Nothing AndAlso sControl.Status = ServiceControllerStatus.Running Then
  Me.Button1.Content = "Stop"
  Button1.BeginStoryBoard(StartAnimation, HandoffBehavior.Compose)
...

Upvotes: 1

Related Questions