简体   繁体   English

在 C# Caliburn Micro WPF 中异步和等待获取 HttpRequest 时更新 ProgressBar

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

I hope to update the progress bar status while downloads string from HttpGet Request.我希望在从 HttpGet Request 下载字符串时更新进度条状态。
While fetching data in OnActivate Methods of Screen Class, progress is not updating.在屏幕 Class 的 OnActivate 方法中获取数据时,进度未更新。

Here is my code of the screen.这是我的屏幕代码。

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);
        }
    }

The xaml of UserControl about this class is like this: 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>

I did fetching Data with HttpGetRequest in OnActivate Method.我确实在 OnActivate 方法中使用 HttpGetRequest 获取数据。

And ProgressBar status Updating is in OnInitialize() Method.而 ProgressBar 状态更新是在 OnInitialize() 方法中。

Please help me to fix update progress bar report while fetching data.请帮助我在获取数据时修复更新进度条报告。

I am a new in Caliburn Micro WPF MVVM.我是 Caliburn Micro WPF MVVM 的新手。 So please guide me how to fix.所以请指导我如何修复。

I used IProgress and send IProgress Status as a Paramter when I call the asynchronize Function.当我调用异步 Function 时,我使用了 IProgress 并将 IProgress 状态作为参数发送。

You can get the best solution in @TimCorey's Video.您可以在@TimCorey 的视频中获得最佳解决方案。

Here is the link.链接在这里。 Asyncronize Downloading with ProgressBar Updating使用 ProgressBar 更新异步下载

I hope this would help you.我希望这会对你有所帮助。

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

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