在 C# Caliburn Micro WPF 中异步和等待获取 HttpRequest 时更新 ProgressBar
Updating ProgressBar while Async and Await fetching HttpRequest in C# Caliburn Micro WPF
我希望在从 HttpGet 请求下载字符串时更新进度条状态。
在屏幕 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);
}
}
这个class的UserControl的xaml是这样的:
<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 的新手。所以请指导我如何修复。
我在调用异步函数时使用了 IProgress 并将 IProgress 状态作为参数发送。
您可以在@TimCorey 的视频中获得最佳解决方案。
这里是link。
Asyncronize Downloading with ProgressBar Updating
希望对您有所帮助。
我希望在从 HttpGet 请求下载字符串时更新进度条状态。
在屏幕 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);
}
}
这个class的UserControl的xaml是这样的:
<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 的新手。所以请指导我如何修复。
我在调用异步函数时使用了 IProgress 并将 IProgress 状态作为参数发送。
您可以在@TimCorey 的视频中获得最佳解决方案。
这里是link。 Asyncronize Downloading with ProgressBar Updating
希望对您有所帮助。