Expander内部的BusyIndicator无法正常工作问题求助
Expander内嵌BusyIndicator无法正常显示的解决方案
你的问题核心是阻塞了UI线程:Thread.Sleep(1000)会直接卡住UI主线程,导致BusyIndicator.IsBusy = true的状态更新根本来不及渲染到界面,等Sleep结束后立刻把IsBusy设为false,自然看不到加载动画。要让BusyIndicator正常工作,必须用异步方式处理耗时操作,保证UI线程始终响应。
修正后的代码
XAML部分(无需改动)
<Grid> <Expander Expanded="Expander_OnExpanded"> <syncfusion:SfBusyIndicator x:Name="BusyIndicator"> <ItemsControl x:Name="ItemsControl"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </syncfusion:SfBusyIndicator> </Expander> </Grid>
C#后台代码(改为异步处理)
private async void Expander_OnExpanded(object sender, RoutedEventArgs e) { BusyIndicator.IsBusy = true; // 模拟异步耗时操作(实际场景可替换为数据库查询、API请求等) await Task.Run(() => { Thread.Sleep(1000); }); // await后自动回到UI线程,直接更新控件 ItemsControl.ItemsSource = new ObservableCollection<string> { "A", "B", "C" }; BusyIndicator.IsBusy = false; }
关键说明
- 用
async/await标记方法,通过Task.Run把耗时操作放到后台线程执行,不会阻塞UI await后的代码会自动切回UI线程,更新控件时不需要额外的线程调度逻辑- 如果你的实际数据加载本身是异步方法(比如HttpClient的请求),直接await该方法即可,不用嵌套Task.Run
内容的提问来源于stack exchange,提问作者OrLevi
相关产品推荐
相关产品推荐

