楼主最近做一个WPF的软件,想用一个全局的弹窗来显示错误信息,就写了一个MessageWindowController类,里面有一个静态方法showWindow(),每当有错误信息的时候,就调用这个方法在屏幕右下角弹一个浮窗来显示错误信息,弹浮窗这个动作由一个wpf动画完成。代码如下:
这是MessageWindowController类 public class MessageWindowController
    {
        private static MessageWindow win;        public static MessageWindow GetWin()
        {
            if (win == null)
            {
                win = new MessageWindow();
            }
            return win;
        }        public static void ShowMessage(string title, string message, bool NotUIThread)
        {
            if (!NotUIThread)
            {
                win.ShowMessage(title, message);
            }
            else
            {
                if (Application.Current == null) return;
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    win.ShowMessage(title, message);
                }), System.Windows.Threading.DispatcherPriority.Send);
            }
        }        public static void Close()
        {
            win.Close();
        }
    }这是浮窗弹出的xaml动画代码     <Storyboard x:Key="OpenStoryboard" Storyboard.TargetName="mGrid">
            <DoubleAnimation From="1" To="0" Duration="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/>
            <DoubleAnimation From="1" To="0" Duration="0:0:0" BeginTime="0:0:0.2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/>
            <ColorAnimation To="#FF000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/>
        </Storyboard>
这是浮窗xaml.cs文件里ShowMessage方法和启动动画的代码 public void ShowMessage(string title, string content)
         {
            if (!IsVisible) //IsVisible是window自带的属性,这里为了判断是否是第一次打开窗口,在当前环境下,这个属性并不能确定窗口是否已经在显示了
            {
                titleLb.Content = title;
                contentTxt.Text = content;
                OpenAni();
                Show();
            }
            else if (actuallyVisible) //actuallyVisible是我自己定义的属性,为了判断窗口是否在显示
            {
                needShow = true;
                titleLb.Content = title;
                contentTxt.Text = content;
            }
            else
            {
                needShow = true;
                titleLb.Content = title;
                contentTxt.Text = content;
                OpenAni();
            }
            timer.Start();
        }        private void OpenAni()
        {
            Storyboard std = this.Resources["OpenStoryboard"] as Storyboard;
            std.Begin();
            actuallyVisible = true;
        }但是我发现调用MessageWindowController.ShowMessage()方法有时候浮窗会弹出,有时候不会,研究了半天也没发现为什么,我怀疑可能是线程调度的问题,还有会不会是wpf动画的某些特性导致的,有高手知道的话解答一下

解决方案 »

  1.   

    我这个弹出实际上不是弹出,就是控制播放一个opacityMask的动画让窗口显示出来,窗口关闭也是,只是用一个动画让它消失(变透明),实际上窗口还是在那里的。而且我这个弹窗是一个独立的window,我每次让它显示只是给它传要显示的信息,它的操作就只有播放动画和修改控件里的text.我不太明白你说的“弹出以后,就不能操作界面上的其他元素”是指不能操作这个提示信息window上的其他元素还是我主窗口的元素呢?
    我现在遇到的问题是,有时候窗口显示着,我去修改它上面显示的信息,也就是给 text 赋其他的字符串,我调试的时候这些代码都执行了,但是窗口上的字不变,这是一个问题;还有一个问题是有时候播放显示窗口的动画,代码执行已经执行过Animation.Begin了,也没有报错,但是窗口不显示