我正在尝试创建一个对话框,显示将某些账单数据从 WPF VB.NET 应用程序加载到 Word 的过程。问题是 WPF UI 没有更新以显示更改。
WPF代码是:
<StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Name="StatusDisplayTextBlock" Text="Printing 30 of 40 to document......" FontSize="20"/>
<ProgressBar Name="PrintProgressBar1" Height="30" Width="400" Margin="0,10,0,0"/>
</StackPanel>
VB.NET 代码:
Imports Microsoft.Office.Interop
Public Class PrintingToDocx
Dim IsWorkDone As Boolean = False
Public Enum BillPrintMode
InOne = 0
Seperate = 1
End Enum
Private BrowserBills As New List(Of Classes.BillsNS.BilBrowserClass)
Private Bills As New List(Of Classes.BillsNS.BillClass)
Private PrintMode As BillPrintMode
Private Sub PrintingToDocx_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
If IsWorkDone = False Then
e.Cancel = True
End If
End Sub
Public Sub New(ByVal BrowserBills As List(Of Classes.BillsNS.BilBrowserClass), ByVal Mode As BillPrintMode)
InitializeComponent()
Me.BrowserBills = BrowserBills
PrintMode = Mode
End Sub
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
GetBills()
If PrintMode = BillPrintMode.InOne Then
PrintInOne()
' IsWorkDone = True
Else
Dim count As Integer = 0
For Each k In Bills
count = count + 1
StatusDisplayTextBlock.Text = "Writing Bill " & count & " of " & Bills.Count & " to WORD....."
DataAccessModuleNS.Reports.PrintaBill(k)
Next
IsWorkDone = True
End If
Me.Close()
End Sub
Private Sub GetBills()
Me.Focus()
StatusDisplayTextBlock.Text = "Reading Bills Data...."
Dim count As Integer = 1
For Each k In BrowserBills
Bills.Add(DataAccessModuleNS.Bills.GetBill(k.ID.ToString))
StatusDisplayTextBlock.Text = "Reading Bill " & count & " of " & BrowserBills.Count & "...."
'count = count + 1
count += 1
Next
End Sub
Private Sub PrintInOne()
Try
StatusDisplayTextBlock.Text = "Writing Bill " & "0" & " of " & Bills.Count & " to WORD....."
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Me.Focus()
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add
Dim countBills As Integer = 0
For Each Bill In Bills
'To Addressing Fields
countBills = countBills + 1
StatusDisplayTextBlock.Text = "Writing Bill " & countBills & " of " & Bills.Count & " to WORD....."
End Sub
End Class
问题是这样的:功能如果工作正常,但直到所有处理完成后 UI 才会显示。以下是处理 word 时任务栏的示例图像:
完成工作后,它可以正常工作,如下所示:
以下是报告
编辑:我现在使用了后台工作人员,但现在的问题是,当进度发生变化并且我尝试更新 UI 线程上的 TextBlock 时,我收到无法在不同线程上执行操作的错误。以下是我在 ProgressChanged 事件中使用的代码。
Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
'Throw New NotImplementedException
StatusDisplayTextBlock.Text = e.UserState
End Sub
您能否帮我弄清楚如何更新 UI 线程。