我有一个 wpf 应用程序,它执行非常繁重的操作,其中用户需要等待应用程序“思考”。
我想要做的是,当应用程序的主线程正在思考时,另一个线程将禁用整个窗口并给它一种灰色的颜色,并且屏幕中间会出现一个圆形进度条。
这是一个大问题,我真的不需要整个代码来做到这一点,只是一般的想法。
感谢你的帮助..
除了上述建议(后台工作人员、调度程序)之外 - 是的,这些是获得您想要的内容的正确技术,但让我讨论一下您在问题中请求的 UI 效果。如果您使用 MVVM 模式,则可以创建一些“我很忙”UI 并绑定到视图模型中的 IsBusy 属性以显示和隐藏 UI。例如:
public class MyViewModel : INotifyPropertyChanged
{
// Bind to this property any UI you want to
// show/hide during long running updates
public bool IsBusy
{
get { return _isBusy; }
set
{
_isBusy = true;
OnPropertyChanged("IsBusy");
}
}
private void OnPropertyChanged(string prop)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
// Note: This is intended to be called on a background thread
private void DoLongRunningOperationOnABackgroundThread()
{
try
{
IsBusy = true;
// do your work
}
finally
{
IsBusy = false;
}
}
}
然后在 UI 中使用这个 xaml (或类似的)
<UserControl:MyControl x:Class="MyApp.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="boolToVis"/>
</UserControl.Resources>
<Grid>
<!-- your UI code goes here -->
<!-- Below this, at higher Z-Order place a control to gray out the screen when IsBusy = true -->
<Border Background="#55000000" BorderThickness="0" Visibility="{Binding IsBusy, Converter={StaticResource boolToVis}}">
<TextBlock Text="I AM BUSY!" Font-Size="32" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White"/>
</Border>
<Grid>
</UserControl>
最终效果是,当您使用后台工作程序或线程池在视图模型中调用 DoLongRunningOperation 函数时,Xaml 中定义的边框将在操作开始/停止时显示/隐藏。您也不需要在此处调用调度程序,因为 WPF 会为您处理线程编组。
网上也有一些带有旋转动画等的繁忙控件的实现,可以为 UI 增添趣味。
此致,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)