简体   繁体   English

UI 不显示 WPF 项目中的 ProgressBar 进度

[英]UI Doesn't Show ProgressBar Progress in WPF Project

i have a Planner Application project, i have created a waiting User Control For The Times When i get the data from Database to show the progress to the user, so i write this codes for this purpose:我有一个 Planner 应用程序项目,当我从数据库中获取数据以向用户显示进度时,我创建了一个等待用户控件,因此我为此目的编写了以下代码:

    public void LoadDestinationList()
    {
        Dispatcher.Invoke(new Action(() =>
        {
            WS_PPB.Value = 10;
            RDFDB_Class MyClass = new RDFDB_Class(TaskType);
            WS_PPB.Value = 25;
            Thread MyThread = new Thread(new ThreadStart(MyClass.LoadDataFromDb));
            WS_PPB.Value = 40;
            MyThread.IsBackground = true;
            MyThread.Start();
            WS_PPB.Value = 55;
            while (MyThread.IsAlive)
            {
                WS_PPB.Value = 75;
            }
            WS_PPB.Value = 100;
        }));
    }

but i don't know why The UserControl Loaded after the data retrieved from the data base and it only show 100% in the progressbar, could you tell me what is wrong here?但我不知道为什么在从数据库检索数据后加载 UserControl 并且它只在进度条中显示 100%,你能告诉我这里有什么问题吗? please help me to fix this problem.请帮我解决这个问题。

i found the solution here: Making a progress bar update in real time in wpf thank you greenjaed.我在这里找到了解决方案:在 wpf 中实时更新进度条,谢谢 greenjaed。 my new codes:我的新代码:

        await Task.Run(() => 
        {

            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 15, DispatcherPriority.Background);
            RDFDB_Class MyClass = new RDFDB_Class(TaskType);
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 30, DispatcherPriority.Background);
            Thread MyThread = new Thread(new ThreadStart(MyClass.LoadDataFromDb));
            MyThread.IsBackground = true;
            MyThread.Start();
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 50, DispatcherPriority.Background);
            while (MyThread.IsAlive)
            {
                WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 75, DispatcherPriority.Background);
            }
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 100, DispatcherPriority.Background);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM