簡體   English   中英

在 C# Caliburn Micro WPF 中異步和等待獲取 HttpRequest 時更新 ProgressBar

[英]Updating ProgressBar while Async and Await fetching HttpRequest in C# Caliburn Micro WPF

我希望在從 HttpGet Request 下載字符串時更新進度條狀態。
在屏幕 Class 的 OnActivate 方法中獲取數據時,進度未更新。

這是我的屏幕代碼。

public class LoadingAccountInfoViewModel:Screen, INotifyPropertyChanged
    {
        private readonly IEventAggregator _eventAggregator;

        // Loading Progrss Bar...
        private int loadingAccountInfoProgressBar;
        public int LoadingAccountInfoProgressBar
        {
            

            get { return loadingAccountInfoProgressBar; }
            set
            {
                if (loadingAccountInfoProgressBar == value)
                {
                    return;
                }
                loadingAccountInfoProgressBar = value;
                NotifyOfPropertyChange(() => LoadingAccountInfoProgressBar);
            }
        }

        public LoadingAccountInfoViewModel(IEventAggregator eventAggregator) {
            _eventAggregator = eventAggregator;
        }

        protected async override void OnInitialize()
        {
            await Task.Factory.StartNew(() =>
            {
                loadingAccountInfoProgressBar = 2;
                for (int i = 0; i < 5; i++)
                {
                    loadingAccountInfoProgressBar += 10;
                }
            }, TaskCreationOptions.AttachedToParent);
        }



        protected override async void OnActivate()
        {
            base.OnActivate();
            User currentUser = Global.currentUser;
            XCService xcService = new XCService();
 
            LiveStream[] liveStreams = await xcService.GetLiveStreams(currentUser.username, currentUser.password, "get_live_streams");
            VodStream[] vodStreams = await xcService.GetVodStreams(currentUser.username, currentUser.password, "get_vod_streams");
            SerieStream[] serieStreams = await xcService.GetSerieStreams(currentUser.username, currentUser.password, "get_series");

            // loadingAccountInfoProgressBar += 10;

            _eventAggregator.PublishOnUIThread(new LoadingInfoDoneMessage());

        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
        }
    }

UserControl的xaml關於這個class是這樣的:

<UserControl x:Class="ProIPTV.Pages.Content.Views.LoadingAccountInfoView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ProIPTV.Pages.Content.Views"
             mc:Ignorable="d" >
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel >
            <Image Source="logo.png" Height="59" />
            <TextBlock Text="s t r i m o" FontSize="68" TextAlignment="Center" Foreground="#808182" Margin="0,0,0,188" />
            <TextBlock Text="LOADING ACCOUNT" FontSize="30" Foreground="#808182" TextAlignment="Center" Margin="0,0,0,62.5"/>
            <ProgressBar x:Name="LoadingAccountInfoProgressBar" Value="{Binding Path=LoadingAccountInfoProgressBar}" Style="{DynamicResource CornerProgressBar}" Height="12" Width="592" Foreground="#80F5F5F5"/>
        </StackPanel>
    </Grid>
</UserControl>

我確實在 OnActivate 方法中使用 HttpGetRequest 獲取數據。

而 ProgressBar 狀態更新是在 OnInitialize() 方法中。

請幫助我在獲取數據時修復更新進度條報告。

我是 Caliburn Micro WPF MVVM 的新手。 所以請指導我如何修復。

當我調用異步 Function 時,我使用了 IProgress 並將 IProgress 狀態作為參數發送。

您可以在@TimCorey 的視頻中獲得最佳解決方案。

鏈接在這里。 使用 ProgressBar 更新異步下載

我希望這會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM