分辨率更改后隐藏的动画窗口是透明的

时间:2012-08-01 13:32:17

标签: c# wpf animation

我正在为窗口的不透明度设置动画

...

  DoubleAnimation myDoubleAnimation =
          new DoubleAnimation(1.0, 0.0, new Duration(TimeSpan.FromSeconds(0.25)), FillBehavior.Stop);
  Storyboard.SetTargetName(myDoubleAnimation, "wndNumpad");
  Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Window.OpacityProperty));
  m_fadeOut = new Storyboard();
  m_fadeOut.Children.Add(myDoubleAnimation);
  m_fadeOut.Completed += new EventHandler(FadeOut_Completed);

...

private void FadeOut_Completed(object sender, EventArgs e)
{
  //  Only hide the running instance
  this.Visibility = System.Windows.Visibility.Hidden;
  // this.Close();
}

如果在FadeOut_Completed()运行后更改了显示器的屏幕分辨率,即窗口的不透明度已设置动画并且窗口被隐藏。然后重新显示窗口将显示几乎透明的窗口。虽然Window.Opacity属性声称不透明度为1,但我会说它隐藏窗口时的不透明度。如果我没有动画但只是将不透明度设置为0并隐藏窗口并在分辨率更改后将不透明度设置为1,窗口按预期重新显示。我还尝试在FadeOut_Completed中将不透明度设置为1。

有没有人知道发生了什么以及如何避免这个问题?

此致 马库斯

1 个答案:

答案 0 :(得分:0)

您必须拥有透明窗口(AllowsTransparency="True"),不可调整大小(ResizeMode="NoResize")并且没有边框(WindowStyle="None")。在C#代码中,我创建了一个DoubleAnimation来改变窗口的不透明度,当它完成时,窗口将被关闭。

XAML代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Background="Red" WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize">
    <Grid>

    </Grid>
</Window>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DoubleAnimation da = new DoubleAnimation();
            da.From = 1;
            da.To = 0;
            da.Duration = new Duration(TimeSpan.FromSeconds(2));
            da.Completed += new EventHandler(da_Completed);
            this.BeginAnimation(OpacityProperty, da);
        }

        void da_Completed(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
相关问题