1

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

...

  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。

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

问候马库斯

4

1 回答 1

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();
        }
    }
}
于 2012-08-01T13:39:12.397 回答