WPF请等待对话

gof*_*flo 6 .net c# wpf wait

我正在开发一个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)

以下是这个问题:

  • 禁用主机控制会导致状态信息丢失(SelectedItems ...)
  • 在线程在显示WaitXUI后几毫秒结束时,WaitXUI有时仅显示几毫秒
  • 虽然线程仍在运行,但有时对话框根本不会出现

这些是我目前想到的主要问题.如何改进这个概念,或者可以采用其他什么方法来解决这个问题?

提前致谢!

She*_*dan 9

在开发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)