WPF .Net 在加载类时更新进度条

问题描述 投票:0回答:2

我正在创建一个 WPF MVVM 应用程序,其中有一个主窗口和一个框架中显示的不同页面。我想添加一个带有进度条的加载屏幕,该进度条会在创建页面实例时更新:

Dim customer_page as new Pg_Custommer

因为有些Pages里面有Databases,在新建Instance的时候会读取,所以需要一些Time。我试图用后台工作者解决这个问题,但这没有用,因为线程无法访问 GUI(没有 STA-Thread)。 我需要一种方法来在每个类加载后更新进度条。

也许有人可以告诉我如何用 Dispatcher(不知道这些是如何工作的)或类似的解决这个问题。

提前致谢和来自瑞士的问候

wpf vb.net mvvm backgroundworker sta
2个回答
0
投票

stackoverflow 上的这篇帖子有解决办法: 解决方案

尝试使用

async tasks
。也可以考虑切换到
C#
.


0
投票

SplashScreen 是一个 WPF 表单,后面只有代码,没有 VMMW,用于测试。所以我可以直接处理进度条值。

这是我的示例代码:

Public Class SplashScreen

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Async Function LoadClassesTask() As Task

        Dim progress = New Progress(Of Integer)(Sub(percent)
                                                    ProgressBar.Value = percent
                                                End Sub)

        Await Task.Factory.StartNew(FDoWork(progress), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext())

    End Function

    Public Shared Function FDoWork(progress As IProgress(Of Integer)) As Action

        Dim i As Integer = 0


        'Create New Instances of Page-Classes

        customerpage = New Pg_Customer
        i += intLoadingProgress ' -> Global Integer = 9
        If progress IsNot Nothing Then progress.Report(i)

        pdfpage = New Pg_PDF_View
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        editorpage = New Pg_Editor
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        accpage = New Pg_Accounting
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        accselectpage = New Pg_AccountingSelect
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        tyreprotokollpage = New Pg_Tyreprotokoll
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbselectpage = New Pg_DatabaseSelect
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbRG = New Pg_Database_RG
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbAP = New Pg_Database_AP
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbMP = New Pg_Database_MP
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbSP = New Pg_Database_SP
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        pganalyzer = New Pg_DocAnalyzer
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dashboardpage = New Pg_Dashboard
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        delivernotespage = New Pg_DeliveryNotes
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        dbCar = New Pg_Database_Car
        i += intLoadingProgress
        If progress IsNot Nothing Then progress.Report(i)

        Return Nothing
    End Function

    Private Sub Btn_Start_Click(sender As Object, e As RoutedEventArgs) Handles Btn_Start.Click

        LoadClassesTask()

    End Sub

End Class

它现在像这样运行,但在所有类实例化后页面仍然冻结并且进度条得到更新。

我想我在某个地方犯了一个非常愚蠢的错误,但我就是找不到它。

© www.soinside.com 2019 - 2024. All rights reserved.