WPF 从 vb.net 代码开始动画给出错误

Posted

技术标签:

【中文标题】WPF 从 vb.net 代码开始动画给出错误【英文标题】:WPF starting animation from vb.net code giving error 【发布时间】:2010-10-15 05:10:39 【问题描述】:

我试图在引发事件(调用函数)时从窗口的 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,...)”之后似乎缺少一些东西

有什么想法吗?

【问题讨论】:

【参考方案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)
...

【讨论】:

做到了,现在没有更多错误了。现在的问题是故事板实际上根本没有开始。按钮的内容也没有。有什么想法吗? @TuxMeister,问题似乎是 FindName 找不到控件。您是否确保该名称已在您正在查看的范围内正确注册? 是的,两个 Storyboard 在 XAML 文件中都有一个 x:Class 和一个 x:Name 属性。当我尝试“FindResource”时它不会首先找到它,但在使用“x:Name”时它会识别它。 我修复了 ContentPresenter 的问题。它被设置为始终默认为特定字符串而不是能够修改它。【参考方案2】:

其实我发现问题出在哪里了:

当我声明动画时,我是在初始化级别进行的,而不是在引发事件时,因此新类实际上是 = 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

结束类

【讨论】:

以上是关于WPF 从 vb.net 代码开始动画给出错误的主要内容,如果未能解决你的问题,请参考以下文章

从 C# 转换为 vb.net 后,它显示错误

VB.net sendmail smtp 错误

使用VB.NET向android模拟器发送通知获取错误401

从 Isnumeric() VB.NET 获取错误

从 VB.NET 向 MySQL 传递和接收输出参数

从 vb.net 将值插入 Access db