[英]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.