You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MahApps Progress Ring刷新数据时无法持续显示问题求助

问题分析

  1. 进度环状态开关被注释:MainViewModel里的IsProgressRingActive控制代码被注释,导致主视图进度环无法激活。
  2. UI线程阻塞风险:若dataRepo.GetData并非真正异步实现,或是OrderBy、数据添加到集合的操作在UI线程执行且耗时较长,会阻塞UI线程,导致进度环来不及渲染就被关闭,看起来像是瞬间消失。
  3. 状态更新时机错误:即使异步操作正常,若关闭进度环的逻辑未等待所有耗时操作完成,也会导致进度环提前消失。

修复方案

1. 启用主视图进度环的状态控制

修改MainViewModel.cs的RefreshData方法,取消状态开关注释,用try/finally确保进度环最终关闭:

public async Task RefreshData()
{
    this.IsProgressRingActive = true;
    try
    {
        DataFileParser parser = new DataFileParser();
        await Task.Run(() => parser.ParseDataFile(Global.DataFileName));
        await parser.UpdateData();
        if (CurrentView.GetType() == typeof(DataViewModel))
        {
            await ServiceLocator.Current.GetInstance<DataViewModel>().LoadData();
        }
    }
    finally
    {
        this.IsProgressRingActive = false;
    }
}

注:必须确保IsProgressRingActive属性实现INotifyPropertyChanged接口,否则UI无法感知状态变化。

2. 优化DataViewModel的LoadData方法

将耗时的数据处理移到后台线程,避免阻塞UI:

public async Task LoadData()
{
    this.IsDataProgressRingActive = true;
    try
    {
        // 获取异步数据
        var datas = await this.dataRepo.GetData(Global.ActiveSite, false, false);
        
        // 后台线程处理排序
        var sortedDatas = await Task.Run(() => datas.OrderBy(f => f.dataID).ToList());
        
        // 回到UI线程更新集合(ObservableCollection必须在UI线程操作)
        Application.Current.Dispatcher.Invoke(() =>
        {
            DataCollection.Clear();
            foreach (var data in sortedDatas)
            {
                DataCollection.Add(data);
            }
        });
    }
    finally
    {
        this.IsDataProgressRingActive = false;
    }
}

3. 确保异步方法的真实异步性

检查dataRepo.GetData和parser.UpdateData:

  • 如果是IO操作(如读文件、数据库查询),确保用原生异步API实现async/await;
  • 如果是纯CPU密集型同步方法,必须用Task.Run包装,放到后台线程执行。

4. 验证属性变更通知

确保进度环绑定的属性正确实现INotifyPropertyChanged,示例:

private bool _isProgressRingActive;
public bool IsProgressRingActive
{
    get => _isProgressRingActive;
    set
    {
        _isProgressRingActive = value;
        OnPropertyChanged();
    }
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

关键注意事项

  • 所有耗时操作必须放在后台线程,避免UI线程阻塞导致进度环无动画;
  • 进度环的状态变更必须触发PropertyChanged通知,否则UI不响应;
  • 用try/finally保证进度环在操作完成(无论成功失败)后关闭,避免出现进度环一直显示的异常情况。

内容的提问来源于stack exchange,提问作者DonDavid12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:34:56