我正在开发一个c#4.0的WPF桌面应用程序,它必须处理许多长时间运行的操作(从数据库加载数据,计算模拟,优化路由等).
当这些长时间运行的操作在后台运行时,我想显示一个Please-Wait对话框.当显示Please-Wait对话框时,应该锁定应用程序,但只是禁用应用程序窗口不是一个好主意,因为所有DataGrids都将失去其状态(SelectedItem
).
到目前为止我的工作但有一些问题:使用Create-factory方法创建一个新的WaitXUI.Create方法需要标题文本和对应该锁定的主机控件的引用.Create方法设置窗口的StartupLocation,标题文本和要锁定的主机:
WaitXUI wait = WaitXUI.Create("Simulation running...", this);
wait.ShowDialog(new Action(() =>
{
// long running operation
}));
Run Code Online (Sandbox Code Playgroud)
使用重载的ShowDialog方法,然后可以显示WaitXUI.ShowDialog重载确实需要一个包装长时间运行操作的Action.
在ShowDialog重载中,我只是在自己的线程中启动Action,然后禁用主机控件(将Opacity设置为0.5并将IsEnabled设置为false)并调用基类的ShowDialog.
public bool? ShowDialog(Action action)
{
bool? result = true;
// start a new thread to start the submitted action
Thread t = new Thread(new ThreadStart(delegate()
{
// start the submitted action
try
{
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
Dispatcher.Invoke(DispatcherPriority.Normal, action);
}
catch (Exception ex)
{
throw ex;
}
finally
{
// close the window
Dispatcher.UnhandledException -= Dispatcher_UnhandledException;
this.DoClose();
}
}));
t.Start();
if (t.ThreadState != ThreadState.Stopped)
{
result = this.ShowDialog();
}
return result;
}
private new bool? ShowDialog()
{
DisableHost();
this.Topmost = true;
return base.ShowDialog();
}
private void DisableHost()
{
if (host != null)
{
host.Dispatcher.Invoke(new Action(delegate()
{
this.Width = host.Width - 20;
host.Cursor = Cursors.Wait;
host.IsEnabled = false;
host.Opacity = 0.5;
}));
}
}
Run Code Online (Sandbox Code Playgroud)
以下是这个问题:
这些是我目前想到的主要问题.如何改进这个概念,或者可以采用其他什么方法来解决这个问题?
提前致谢!
在开发WPF应用程序时,一点横向思维总是有帮助的.只需a Grid
,a Rectangle
,a bool
属性(您已经拥有)和a 即可轻松满足您的要求BooleanToVisibilityConverter
,您不必禁用任何控件.
这个想法很简单.Rectangle
在视图内容前面添加一个白色,其Opacity
属性设置在其0.5
周围0.75
.数据将其Visibility
属性绑定到bool
视图模型或代码后面的属性并插入BooleanToVisibilityConverter
:
<Grid>
<Grid>
<!--Put your main content here-->
</Grid>
<Rectangle Fill="White" Opacity="0.7" Visibility="{Binding IsWaiting,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<!--You could add a 'Please Wait' TextBlock here-->
</Grid>
Run Code Online (Sandbox Code Playgroud)
现在,当您要禁用控件时,只需将bool
属性设置为true
,Rectangle
将使UI显示为淡化:
IsWaiting = true;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15369 次 |
最近记录: |