非阻塞加载窗口

时间:2016-07-18 15:42:27

标签: c# wpf

我在实现非阻塞加载窗口时遇到了一些问题。这是要求指定的内容。

enter image description here

基本上每次有加载事件时,此窗口应该在模态时出现在顶部,然后当它收到加载完成事件时,它应该再次隐藏窗口。我已经看到很多使用叠加层的建议。然而,我们可以使用许多窗口和对话框,因此对所有窗口和对话框执行操作都是不切实际的。

这是我的第一次刺伤。问题是,如果我不打电话给ShowDialog并使用show而不是窗口出现,但它的白色,完全空白。如果我使用ShowDialog,窗口就会永远存在,因为它是线程阻塞的。

public partial class MetroProgressWindow : MetroWindow, INotifyPropertyChanged
{
    private string _message;
    private bool _isRunning;

    private static MetroProgressWindow _instance;

    public static MetroProgressWindow Instance()
    {
        if (_instance == null)
        {
            _instance = new MetroProgressWindow();
        }
        return _instance;
    }

    private MetroProgressWindow()
    {
        InitializeComponent();

        // register event listener
        Messenger.Default.Register<ProgressIndicatorEvent>(this, OnReceivedMessage);
    }

    private void OnReceivedMessage(ProgressIndicatorEvent e)
    {
        if (e.Type == ProgressIndicatorEvent.ProgressIndicatorEventTypes.START)
        {
            Message = e.Message;
            _isRunning = true;
            this.Show();
            // this.ShowDialog was causing app to hang because thread blocking
        }
        else
        {
            Message = null;
            _isRunning = false;
            this.Hide();
        }
    }

    public string Message
    {
        get { return _message; }
        private set
        {
            _message = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

可以使用多个UI线程实现吗?我发现这是令人气馁的,但在这种情况下,由于我没有更新数据或其他任何内容,我认为这不是什么大问题。

0 个答案:

没有答案
相关问题