开发者

NotificationWindow only one is visible at a time

            NotificationWindow notify = new NotificationWindow();
            NotifyWindow win = new NotifyWindow();
            win.Header.Text = "Custom Message Header";
            win.Description.Text = "Th开发者_高级运维is is a custom description.";
            notify.Width = win.Width;
            notify.Height = win.Height;
            notify.Content = win;
            notify.Show(5000); 

When this program have been execute 3 times,[NotificationWindow only one is visible at a time]Error is appear by notify.Show(5000).how do I solve this problem??


You need to maintain a queue of notifications so that each notification appears after another. To do that you'll need manage such a queue with your own code.

Here is such a notification queue manager I've knocked up.

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);
    }
}

You can adjust your code to:-

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

If you call such code repeatedly you will just get multiple notifications (although it might be hard to tell if the text doesn't change).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜