Last active
June 11, 2020 17:18
-
-
Save azureskydiver/672ff1958b3c2fa208a6f2e4e2bde5ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="TestWpfFade.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:TestWpfFade" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="450" Width="800" | |
Opacity="0.0" | |
Loaded="Window_Loaded" | |
MouseDoubleClick="Window_MouseDoubleClick"> | |
<StackPanel> | |
<TextBlock>Hello, World!!!</TextBlock> | |
</StackPanel> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
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.Animation; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Windows.Threading; | |
namespace TestWpfFade | |
{ | |
public partial class MainWindow : Window | |
{ | |
bool _shown = false; | |
bool _animating = false; | |
public MainWindow() => InitializeComponent(); | |
private void Fade(double from, double to) | |
{ | |
if (_animating) | |
return; | |
DoubleAnimation animation = new DoubleAnimation | |
{ | |
To = to, | |
From = from, | |
BeginTime = TimeSpan.FromSeconds(0), | |
Duration = TimeSpan.FromSeconds(3), | |
FillBehavior = FillBehavior.Stop | |
}; | |
animation.Completed += (o, e) => | |
{ | |
Opacity = to; | |
_shown = !_shown; | |
_animating = false; | |
}; | |
_animating = true; | |
Opacity = from; | |
BeginAnimation(OpacityProperty, animation); | |
} | |
void FlipSwitch() | |
{ | |
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => FlipReally())); | |
void FlipReally() | |
{ | |
if (!_shown) | |
Fade(0.0, 1.0); | |
else | |
Fade(1.0, 0.0); | |
} | |
} | |
private void Window_Loaded(object sender, RoutedEventArgs e) => FlipSwitch(); | |
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e) => FlipSwitch(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment