我正在为窗口的不透明度设置动画
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.OOpacity属性声称不透明度为1。如果我不设置动画,只是简单地将不透明度设置为0并隐藏窗口,并且在分辨率更改后将不透明度重新设置为1,则窗口将按预期重塑。我还尝试在FadeOut_Completed中将不透明度设置回1。
有人知道发生了什么吗?我该如何避免这个问题?
问候Markus
必须具有透明窗口(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();
}
}
}