Replace DelayInvoke with Task + Async

This commit is contained in:
bao-qian
2016-01-06 06:31:17 +00:00
parent b78e0144de
commit 1a8efdbf2c
6 changed files with 88 additions and 137 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
@@ -7,71 +8,81 @@ using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Wox.Helper;
namespace Wox {
public partial class Msg : Window {
Storyboard fadeOutStoryboard = new Storyboard();
private bool closing = false;
namespace Wox
{
public partial class Msg : Window
{
Storyboard fadeOutStoryboard = new Storyboard();
private bool closing = false;
public Msg() {
InitializeComponent();
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this,
public Msg()
{
InitializeComponent();
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this,
screen.WorkingArea.Width,
screen.WorkingArea.Height);
Left = dipWorkingArea.X - this.Width;
Top = dipWorkingArea.Y;
showAnimation.From = dipWorkingArea.Y;
showAnimation.To = dipWorkingArea.Y - Height;
screen.WorkingArea.Height);
Left = dipWorkingArea.X - this.Width;
Top = dipWorkingArea.Y;
showAnimation.From = dipWorkingArea.Y;
showAnimation.To = dipWorkingArea.Y - Height;
// Create the fade out storyboard
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3))) {
AccelerationRatio = 0.2
};
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
// Create the fade out storyboard
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3)))
{
AccelerationRatio = 0.2
};
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
imgClose.Source = new BitmapImage(new Uri("Images\\close.pn", UriKind.Relative));
//imgClose.Source = new BitmapImage(new Uri(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Images\\close.png")));
imgClose.MouseUp += imgClose_MouseUp;
}
imgClose.Source = new BitmapImage(new Uri("Images\\close.pn", UriKind.Relative));
//imgClose.Source = new BitmapImage(new Uri(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Images\\close.png")));
imgClose.MouseUp += imgClose_MouseUp;
}
void imgClose_MouseUp(object sender, MouseButtonEventArgs e) {
if (!closing) {
closing = true;
fadeOutStoryboard.Begin();
}
}
void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
{
if (!closing)
{
closing = true;
fadeOutStoryboard.Begin();
}
}
private void fadeOutStoryboard_Completed(object sender, EventArgs e) {
Close();
}
private void fadeOutStoryboard_Completed(object sender, EventArgs e)
{
Close();
}
public void Show(string title, string subTitle, string icopath) {
tbTitle.Text = title;
tbSubTitle.Text = subTitle;
if (string.IsNullOrEmpty(subTitle))
{
tbSubTitle.Visibility = Visibility.Collapsed;
}
if (!File.Exists(icopath)) {
imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative));
}
else {
imgIco.Source = new BitmapImage(new Uri(icopath));
}
public void Show(string title, string subTitle, string icopath)
{
tbTitle.Text = title;
tbSubTitle.Text = subTitle;
if (string.IsNullOrEmpty(subTitle))
{
tbSubTitle.Visibility = Visibility.Collapsed;
}
if (!File.Exists(icopath))
{
imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative));
}
else {
imgIco.Source = new BitmapImage(new Uri(icopath));
}
Show();
Show();
Dispatcher.DelayInvoke("ShowMsg",
() => {
if (!closing) {
closing = true;
Dispatcher.Invoke(new Action(fadeOutStoryboard.Begin));
}
}, TimeSpan.FromSeconds(3));
}
}
Dispatcher.InvokeAsync(async () =>
{
if (!closing)
{
closing = true;
await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin);
}
});
}
}
}