views:

1043

answers:

2

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?

+1  A: 

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)
...
JaredPar
that did it, no more error now. Problem now is that storyboard is actually not beggining at all. Neither does the content of the button. Any ideas?
TuxMeister
@TuxMeister, There problem appears to be that FindName is failing to find the control. Have you made sure the name is properly registered at the scope you are looking at?
JaredPar
Yeah, both Storyboards have a x:Class and an x:Name property in the XAML file. It wouldn't find it first as I tried "FindResource" but it recognized it when using "x:Name".
TuxMeister
I fixed the issue with the ContentPresenter. It was set to some how always default to a particular string instead of being able to modify it.
TuxMeister
A: 

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

TuxMeister