0

我正在尝试创建一个对话框,显示将某些账单数据从 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 线程。

4

1 回答 1

0

您需要从 UI 线程中移动逻辑操作...

最好的解决方案是使用Backgroundworker类将所有操作与 UI 分开。可以在这里找到很好的教程[链接]

如果通过更改可以使用 .NET 4.5,则可以简单地使用Async- Await模式 [ Async in VB ]

于 2012-12-22T12:59:58.343 回答