NotificationWindow一次只能显示一个

时间:2011-09-09 02:32:36

标签: silverlight visual-studio-2010 silverlight-4.0

            NotificationWindow notify = new NotificationWindow();
            NotifyWindow win = new NotifyWindow();
            win.Header.Text = "Custom Message Header";
            win.Description.Text = "This is a custom description.";
            notify.Width = win.Width;
            notify.Height = win.Height;
            notify.Content = win;
            notify.Show(5000); 

当这个程序执行了3次时,[NotificationWindow一次只能看到一个]通过notify.Show(5000)出现错误。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要维护一个通知队列,以便每个通知都显示在另一个通知之后。要做到这一点,您需要使用自己的代码管理这样的队列。

这是一个我已经敲响的通知队列管理器。

public static class NotificationManager
{
    private static Queue<FrameworkElement> queue = new Queue<FrameworkElement>();
    private static NotificationWindow window = new NotificationWindow();
    private static int duration = 5000;

    static NotificationManager()
    {
        window.Closed += window_Closed;
    }

    public static void Notify(FrameworkElement content)
    {
        if (Deployment.Current.Dispatcher.CheckAccess())
        {
            if (window.Visibility == Visibility.Collapsed && queue.Count == 0)
            {
                Show(content);
            }
            else
            {
                queue.Enqueue(content);
            }
        }
        else
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => Notify(content));
        }
    }

    public static void CloseCurrentNotification()
    {
        window.Close();
    }

    private static void window_Closed(object sender, EventArgs e)
    {
        if (queue.Count > 0)
        {
            Show(queue.Dequeue());
        }
    }

    private static void Show(FrameworkElement content)
    {
        window.Content = content;
        window.Height = content.Height;
        window.Width = content.Width;
        window.Show(duration);
    }
}

您可以将代码调整为: -

            NotifyWindow win = new NotifyWindow();
            win.Header.Text = "Custom Message Header";
            win.Description.Text = "This is a custom description.";
            NotificationManager.Notify(win);

如果你反复调用这样的代码,你只会收到多个通知(尽管可能很难判断文本是否没有改变)。