Reputation: 1484
I make a Wpf-modeless-reusable window with showing animation. It' was reused for loading performance. And then a new problem has occured.
At first, it is a MainWindow's code behind. win1 is instance of popup window.
public partial class MainWindow : Window
{
Window1 win1 = new Window1();
public MainWindow() { InitializeComponent(); }
private void button1_Click(object sender, RoutedEventArgs e) { win1.Owner = this; win1.Show(); }
private void button2_Click(object sender, RoutedEventArgs e) { win1.Hide(); }
private void Window_Closed(object sender, EventArgs e) { win1.Close(); }
}
And here is a popup window's xaml...
<Window x:Class="WpfApplication4.Window1" x:Name="win"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" ShowActivated="False" Background="#00000000" AllowsTransparency="True" WindowStyle="None" IsVisibleChanged="Window_IsVisibleChanged">
<Window.Resources>
<Storyboard x:Key="aniShowing" FillBehavior="Stop">
<ParallelTimeline BeginTime="0:0:0" Duration="0:0:2">
<DoubleAnimation Storyboard.TargetName="win" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:2"/>
</ParallelTimeline>
</Storyboard>
</Window.Resources>
<Ellipse Fill="Red"/>
</Window>
... and finally, here is a code behind of popup window.
public partial class Window1 : Window
{
Storyboard aniShowing;
public Window1()
{
InitializeComponent();
aniShowing = (Storyboard)this.Resources["aniShowing"];
}
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true) { this.BeginStoryboard(aniShowing); }
else { aniShowing.Remove(this); this.Opacity = 0; }
}
}
I want to show showing animation at Window_IsVisibleChanged. It works very well. But it's a first time only.
In second time, win1 showed with blink at shortly, and animation was played.
third time, fourth time looks like second time, too.
how can i get rid this mysterious blink?
Upvotes: 0
Views: 1285
Reputation: 16628
I'm guessing your problem actually is that you don't have a animation that fades out the said window, so when the button1 is clicked, it does the animation,
Storyboard.TargetProperty="Opacity" From="0" To="1"
Whatever the state it is in, it will go from 0 to 1, you see a "blink" that sets it to 0 before the animation can apply if it's already at 1.
To get rid of it, do the reverse animation when the window is closed (Opacity from 1 to 0 on button2 click i believe).
PS: Hide() is the same as setting win.Visibility = Visibility.Hidden;
you didn't take into account Visibility.Collapsed.
Upvotes: 1