2009-03-24 6 views
0

Je suis en train de déclencher une animation déclarée dans le fichier XAML à partir du code de vb de la fenêtre de la fenêtre lorsqu'un événement est déclenché (appel d'une fonction), comme événement « chargé » d'une fenêtre .WPF à partir animation à partir du code vb.net donnant erreur

Voilà comment je déclare l'animation (comme un story-board):

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

Et voici le code de la fonction qui échoue:

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 

L'erreur que je reçois est le suivant:

"La valeur ne peut être nULL nom du paramètre:. story-board"

On dirait qu'il manque quelque chose juste après « Button1.BeginStoryboard (DebuterAnimation, ...)

Toutes les idées?

Répondre

1

Il ressemble à la valeur DebuterAnimation rien qui est à l'origine de l'exception d'être jeté. Vous devez vérifier que c'est non-Nothing avant d'appeler BeginStoryBoard.

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

qui l'a fait, plus d'erreur maintenant. Le problème est maintenant que le storyboard n'est en réalité pas du tout débutant. Le contenu du bouton ne l'est pas non plus. Des idées? – TuxMeister

+0

@TuxMeister, Il problème semble être que FindName ne parvient pas à trouver le contrôle. Vous êtes-vous assuré que le nom est correctement enregistré sur le périmètre que vous regardez? – JaredPar

+0

Oui, les deux storyboards ont un x: Class et un x: propriété Name dans le fichier XAML. Je ne l'aurais pas trouvé en premier lorsque j'ai essayé "FindResource" mais il l'a reconnu en utilisant "x: Name". – TuxMeister

0

En fait, je compris ce que le problème était:

Quand je déclarais l'animation que je l'ai fait au niveau de l'initialisation, pas quand l'événement a été raized afin que la nouvelle classe était en réalité = NULL.

L'astuce consiste à coller dans le code logique au lieu de la partie de la déclaration pour que cela fonctionne. Voici le code final (il workes tout simplement génial):

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

Questions connexes