小编gof*_*flo的帖子

WPF请等待对话

我正在开发一个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();
        }
    })); …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf wait

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

wait ×1

wpf ×1