none
WPF渐变窗体的实现 RRS feed

  • 问题

  •         想在WPF实现一个渐变窗体,从屏幕中间向四周扩展,直至布满整个屏幕。我用Animation来实现,但是达不到效果,运行结束后只在屏幕上方出现一条线。单独实现Width或者Height的渐变动画没问题,同时的话,就存在上面的现象。不知是哪里设置不对?求解,下面是我的代码:

            private double width = SystemParameters.PrimaryScreenWidth;
            private double height = SystemParameters.PrimaryScreenHeight;

            public NavigateWindow()
            {
                InitializeComponent();

                Left = width / 2;
                Top = height / 2;
                Width = 0;
                Height = 0;
                WindowStyle = System.Windows.WindowStyle.None;
                ResizeMode = System.Windows.ResizeMode.NoResize;
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                DoubleAnimation daWidth = new DoubleAnimation();
                daWidth.From = Width;
                daWidth.To = width;
                daWidth.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                daWidth.AccelerationRatio = 0.5;

                DoubleAnimation daLeft = new DoubleAnimation();
                daLeft.From = Left;
                daLeft.To = 0;
                daLeft.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                daLeft.AccelerationRatio = 0.5;

                DoubleAnimation daHeight = new DoubleAnimation();
                daHeight.From = Height;
                daHeight.To = height;
                daHeight.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                daHeight.AccelerationRatio = 0.5;

                DoubleAnimation daTop = new DoubleAnimation();
                daTop.From = Top;
                daTop.To = 0;
                daTop.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                daTop.AccelerationRatio = 0.5;

                BeginAnimation(LeftProperty, daLeft);
                BeginAnimation(TopProperty, daTop);
                BeginAnimation(WidthProperty, daWidth);
                BeginAnimation(HeightProperty, daHeight);
            }

    2012年11月11日 14:48