Silverlight Mouse Events: MouseEnter and MouseLeave conflicts - silverlight

I have a collection of buttons in a grid. For each one of these buttons, I want to handle the MouseEnter and MouseLeave events to animate the height of the button (and do some other interesting stuff). It all works good until I start moving my mouse too fast over and off the buttons which eventually cause the events to take place at before the other is complete. What's the best way of making sure the events wait for eachother before being triggered?
UPDATE:
Going by x0r's advice, I refactored my code into an internal class which inherits from Button and has the required methods to perform the animations. Unfortunately, this did not really solve the problem because - I think - I'm handling the Completed event of the first animation in two separate places. (correct me if I'm wrong). Here's my code:
internal class MockButton : Button
{
#region Fields
private Storyboard _mouseEnterStoryBoard;
private Storyboard _mouseLeaveStoryBoard;
private Double _width;
#endregion
#region Properties
internal Int32 Index { get; private set; }
#endregion
#region Ctors
internal MockButton(Int32 index) : this(index, 200)
{
}
internal MockButton(Int32 index, Double width)
{
this.Index = index;
this._width = width;
}
#endregion
#region Event Handlers
internal void OnMouseEnter(Action action, Double targetAnimationHeight)
{
if (_mouseEnterStoryBoard == null)
{
_mouseEnterStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.From = 10;
heightAnimation.To = targetAnimationHeight;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseEnterStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseEnterStoryBoard.Children.Add(heightAnimation);
}
_mouseEnterStoryBoard.Completed += (s, e) =>
{
action.Invoke();
};
_mouseEnterStoryBoard.Begin();
}
internal void OnMouseLeave()
{
if (_mouseLeaveStoryBoard == null)
{
_mouseLeaveStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = 10;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseLeaveStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseLeaveStoryBoard.Children.Add(heightAnimation);
}
if (_mouseEnterStoryBoard.GetCurrentState() != ClockState.Stopped)
{
_mouseEnterStoryBoard.Completed += (s, e) =>
{
_mouseLeaveStoryBoard.Begin();
};
}
else
{
_mouseLeaveStoryBoard.Begin();
}
}
#endregion
}
UPDATE 2:
Some events are getting triggered multiple times. An example of that is the Click event on the close button of my Rule object...
public Rule(Action<Int32> closeAction)
{
this.Style = Application.Current.Resources["RuleDefaultStyle"] as Style;
this.CloseAction = closeAction;
this.Loaded += (s, e) =>
{
if (_closeButton != null)
{
_closeButton.Click += (btn, args) =>
{
if (this.CloseAction != null)
{
this.CloseAction.Invoke(this.Index);
}
};
if (_closeButtonShouldBeVisible)
{
_closeButton.Visibility = System.Windows.Visibility.Visible;
}
}
};
}
And below is the Action<Int32> I'm passing to the Rule object as the CloseAction:
private void RemoveRule(Int32 ruleIndex)
{
Rule ruleToRemove = Rules.FirstOrDefault(x => x.Index.Equals(ruleIndex));
Storyboard sb = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
sb.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
animation.Duration = TimeSpan.FromMilliseconds(300);
animation.From = 1;
animation.To = 0;
sb.Children.Add(animation);
Storyboard.SetTarget(animation, ruleToRemove);
sb.Completed += (s, e) =>
{
if (Rules.FirstOrDefault(x => x.Index.Equals(ruleIndex)) != null)
{
this.Rules.RemoveAt(ruleIndex);
}
};
sb.Begin();
}
UPDATE 3:
In order to avoid the animations running too early, I thought I could delay the MouseEnter event, so if the user just scrolls over the item too fast, it doesn't kick off. But I have a problem now: Say the user mouses over the item and then mouses out. If I use the Storyboard.BeginTime property, that won't safe guard against that behavior because eventhough the animation gets delayed, it's still going to start eventually... So is there a way I could prevent that from happening?
Any suggestions?

check in your mouseleave eventhandler if the first storyboard is still running and if that is the case attach the starting of the second storyboard to the Completed event of the first storybaord:
private void OnOddRowMouseLeave(object sender, MouseEventArgs e)
{
...
if(_firstStoryboard.GetCurrentState() != ClockState.Stopped)
_firstStoryboard.Completed += (s,e) => _secondStoryboard.Begin();
else
_secondStoryboard.Begin()

Everything that Silverlight does is asyncronous and so most likely what is happening is that because you are moving quickly in and out of the box the mouse leave is being fired before the mouseenter has a chance to finish. You could setup your two events so thay they have an indicator of whether or not the other is in process. For example you could do this
bool mouseOut =false;
bool mouseIn =false;
void OnMouseEnter(Action action, Double targetAnimationHeight)
{
if(!this.mouseOut)
{
this.mouseIn = true;
if (_mouseEnterStoryBoard == null)
{
_mouseEnterStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.From = 10;
heightAnimation.To = targetAnimationHeight;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseEnterStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseEnterStoryBoard.Children.Add(heightAnimation);
}
_mouseEnterStoryBoard.Completed += (s, e) =>
{
action.Invoke();
};
_mouseEnterStoryBoard.Begin();
if(this.mouseOut)
{
this.OnMouseLeave();
}
this.mouseIn = false;
}
}
void OnMouseLeave()
{
if(!this.mouseIn)
{
this.mouseOut = false;
if (_mouseLeaveStoryBoard == null)
{
_mouseLeaveStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = 10;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseLeaveStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseLeaveStoryBoard.Children.Add(heightAnimation);
}
if (_mouseEnterStoryBoard.GetCurrentState() != ClockState.Stopped)
{
_mouseEnterStoryBoard.Completed += (s, e) =>
{
_mouseLeaveStoryBoard.Begin();
};
}
else
{
_mouseLeaveStoryBoard.Begin();
}
}
else
{
this.mouseOut = true;
}
}
I haven't actually checked this code but this should help you to at least get closer to what you want. This should be quick enough that your user doesn't realize that it is not firing exactly on exit if they go over it quickly. But this should help to keep you from getting overlap.
Another way you could do this is setup the initial events as null, and have the mouse in event set the mouse in event when it is complete but the problem with that is that if the mouse out fires before the event is set then you don't event get the event firing.

Related

Prevent RepositoryItemSearchLookUpEdit when Popup is Open When CloseUpKey.Key is pressed

I use a RepositoryItemSearchLookUpEdit. its CloseUpKey property is set to space
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
I want to use this shortcut only for open popup and not for closing popup.
How can I achieve this?
UPDATE------------------------
First I create an RepositoryItemSearchLookUpEdit object
var result = new RepositoryItemSearchLookUpEdit();
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
result.KeyDown += repositoryItemLookUpEdit_KeyDown;
result.CloseUp += repositoryItemLookUpEdit_CloseUp;
result.QueryCloseUp += repositoryItemLookUpEdit_QueryCloseUp;
private void repositoryItemLookUpEdit_QueryCloseUp(object sender, System.ComponentModel.CancelEventArgs e)
{
var edit = sender as SearchLookUpEdit;
KeyEventArgs k = new KeyEventArgs(edit.Properties.CloseUpKey.Key);
AttachKeyPressEvent(k);
if (k.KeyCode == edit.Properties.CloseUpKey.Key)
e.Cancel = true;
}
And pass it to a grid column:
grdListView.Columns["yyy"].ColumnEdit = result
How can I achieve that with these events, without creating a descendant SearchLookUpEdit
UPDATED:
The problem is that CloseUp event (where you could get the necessary info about the closeup key) occurs after the QueryCloseUp event (where you could precent the closing up event). Also the KeyPress, KeyDown and KeyUp events seem also NOT to occur when the QueryCloseUp occurs, as a result they couldn't be overridden. So I tried this, I created a custom KeyEventHandler and triggered him during QueryCloseUp event in order to get the necessary info of what key was pressed and cancel the event if the close key event was the one. Here is my codeTry it to see if it suits you
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Here you can add your grid control as you have created
DataTable dt = new DataTable();
dt.Columns.Add("ID"); //use your own names and types
gridControl1.DataSource = dt;
var result = new RepositoryItemSearchLookUpEdit();
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
result.QueryCloseUp += new CancelEventHandler(repositoryItemLookUpEdit_QueryCloseUp);
((gridControl1.MainView as GridView).Columns["ID"] as GridColumn).ColumnEdit = result;
}
private static readonly object myQueryCloseUp = new object();
public event KeyEventHandler MyQueryCloseUp
{
add { Events.AddHandler(myQueryCloseUp, value); }
remove { Events.RemoveHandler(myQueryCloseUp, value); }
}
protected virtual void AttachKeyPressEvent(KeyEventArgs e)
{
KeyEventHandler handler = (KeyEventHandler)Events[myQueryCloseUp];
if (handler != null)
handler(this, e);
}
//Here you add your own Handler implementation
public void repositoryItemLookUpEdit_QueryCloseUp(object sender, CancelEventArgs e)
{
KeyEventArgs k = new KeyEventArgs((sender as SearchLookUpEdit).Properties.CloseUpKey.Key);
AttachKeyPressEvent(k);
if (k.KeyCode == (sender as SearchLookUpEdit).Properties.CloseUpKey.Key)
e.Cancel = true;
}
}

Proper disposal of RX subscriptions

I'm new to Reactive Extensions, and this is what I've come up with to do a Popup Toaster Notification. When the mouse goes over the toaster, the opacity is brought back to 100%. Otherwise, it fade out gradually.
The code works, but I'm not entirely confident I'm not leaking resources, especially in the mouseOut subscription. In addition, I'm not sure if this is the best way to implement this functionality.
Any critiques, tips would be appreciated.
private void rxPop()
{
Rectangle toaster = (Rectangle)this.FindName("toaster1");
Thickness newToasterPosition = new Thickness(
toaster.Margin.Left, toaster.Margin.Top,
toaster.Margin.Right, toaster.Margin.Bottom + toaster.Height);
/* Animations */
Storyboard slideInAnimation = slide(toaster,
newToasterPosition,
TimeSpan.FromMilliseconds(450));
Storyboard fadeInAnimation = animateOpacity(toaster, 1.0, TimeSpan.FromMilliseconds(150));
Storyboard fadeOutAnimation = animateOpacity(toaster, 0.0, TimeSpan.FromSeconds(3));
/* Events */
var mouseEnter = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseEnter += h,
h => toaster.MouseEnter -= h);
var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseLeave += h,
h => toaster.MouseLeave -= h);
var slideInCompleted = Observable.FromEventPattern<EventHandler, EventArgs>(
h => slideInAnimation.Completed += h,
h => slideInAnimation.Completed -= h);
var fadeOutCompleted = Observable.FromEventPattern<EventHandler, EventArgs>(
h => fadeOutAnimation.Completed += h,
h => fadeOutAnimation.Completed -= h);
// slideIn then fadeOut
slideInCompleted.Subscribe(e => fadeOutAnimation.Begin());
var mouseEnterSubscription = mouseEnter
.ObserveOnDispatcher()
.Do(a =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
mouseOut.Do(
b =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
}).Subscribe();
})
.Subscribe();
fadeOutCompleted.Subscribe((e) => mouseEnterSubscription.Dispose());
slideInAnimation.Begin();
}
Ideally, I would have liked to express the events in the following manner:
slideIn then fadeOut
unless mouseEnter
then fadeIn , slideIn.Pause
until mouseLeave
then fadeOut.Begin and slideIn.Resume
What's the closest way to do this in RX?
UPDATE #1
*UPDATE #2* (Cleaning up Subscribe())
Here's a somewhat cleanear attempt.
protected CompositeDisposable _disposables = new CompositeDisposable();
private void rxPop()
{
IDisposable mouseEnterSubscription = null;
/* Business logic: slideIn then fadeOut then remove from visual tree */
_disposables.Add(
slideInAnimation
.BeginUntilDone()
.Select(slideInCompletedEvent =>
fadeOutAnimation.BeginUntilDone())
.Switch()
.Subscribe(fadeOutAnimationCompletedEvent =>
{
mouseEnterSubscription.Dispose();
// remove from visual tree
(toaster.Parent as Panel).Children.Remove(toaster);
}));
/* Business logic: mouseEnter/mouseLeave should pause/restart animations */
mouseEnterSubscription = mouseEnter
.ObserveOnDispatcher()
.Do(mouseEnterEventArgs =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
})
.Select(e => mouseOut)
.Switch()
.Do(mouseLeaveEventArgs =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
})
.Subscribe();
}
public static class RxExtensions
{
public static IObservable<EventPattern<EventArgs>> BeginUntilDone(this Storyboard sb)
{
var tmp = Observable.FromEventPattern(
h => sb.Completed += h,
h => sb.Completed -= h);
sb.Begin();
return tmp;
}
}
My questions are:
Is ObserveOnDispatcher() done correctly?
Does the Switch() dispose the previous IObservable for me?
I struggle to translate the above into LINQ query syntax
/* Business Logic */
var showToast =
// Start by sliding in
from slideInComplete in slideIn.BeginObservable()
where slideInComplete
// Then in parallel, fadeOut as well as wait for mouseEnter
from fadeOutComplete in fadeOut.BeginObservable()
from enter in mouseEnter
// ... I'm lost here.
// ... how do I express
// ..... mouseEnter should pause fadeOut?
select new Unit();
Well, first off, you're leaking IDisposables all over the place - each one of those Subscribe calls returns an IDisposable, which are just going out of scope, not being disposed properly. That part is easily fixed, however, using some of the IDisposable containers in the Rx lib:
(snippets from the test harness I threw together around your example code)
// new fields
// A serial disposable lets you wrap one disposable
// such that changing the wrapped disposable autocalls
// Dispose on the previous disposable
protected SerialDisposable _resubscriber;
// A composite disposable lets you track/dispose a whole
// bunch of disposables at once
protected CompositeDisposable _disposables;
// no real need to do this here, but might as well
protected void InitializeComponent()
{
_disposables = new CompositeDisposable();
_resubscriber = new SerialDisposable();
// misc
this.Unloaded += (o,e) =>
{
if(_disposables != null) _disposables.Dispose();
if(_resubscriber != null) _resubscriber.Dispose();
};
Then later on in your queries, wrap all Subscribe calls (except one, see below) in something like this:
// slideIn then fadeOut
_disposables.Add(slideInCompleted.Subscribe(e => fadeOutAnimation.Begin()));
The one exception being the MouseOut "canceller":
.Do(a =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
_resubscriber.Disposable = mouseOut.Do(
b =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
}).Subscribe();
})
Now...as for this:
slideIn then fadeOut
unless mouseEnter
then fadeIn , slideIn.Pause
until mouseLeave
then fadeOut.Begin and slideIn.Resume
That I'll have to think about...I think there's a more...rx way of doing it, but I'd have to ponder it a bit. Definitely handle the IDisposable cleanup, tho!
(will edit this if I can come up with something for the second bit)
EDIT: Ooh, think I've got something promising...
First off, let's rig up a way to translate Storyboard start/completes into an IObservable:
public static class Ext
{
public static IObservable<bool> BeginObservable(this Storyboard animation)
{
var sub = new BehaviorSubject<bool>(false);
var onComplete = Observable.FromEventPattern<EventHandler, EventArgs>(
h => animation.Completed += h,
h => animation.Completed -= h);
IDisposable subscription = null;
subscription = onComplete.Subscribe(e =>
{
Console.WriteLine("Animation {0} complete!", animation.Name);
sub.OnNext(true);
if(subscription != null)
subscription.Dispose();
});
Console.WriteLine("Starting animation {0}...", animation.Name);
animation.Begin();
return sub;
}
}
Basically, this sets up a "Start Animation, and signal us true when it's done" sequence...on to the good part!
So let's assume you've got the following Storyboards defined:
fadeIn: Animates Opacity to 1.0
fadeOut: Animates Opacity to 1.0
slideIn: Animates Margin to "in" value
slideOut: Animates Margin to "out" value
And these observables (slight name tweaks from your code):
mouseEnter: => MouseEnter event
mouseOut: => MouseLeave event
You can set up an IObservable that carries the actual sequence thusly:
var showToast =
// Start this mess on a "mouse enter" event
from enter in mouseEnter
// Start (in parallel) and wait until the fadeIn/slideIn complete
from fadeInComplete in fadeIn.BeginObservable()
from slideInComplete in slideIn.BeginObservable()
where fadeInComplete && slideInComplete
// Until you see a "mouse out" event
from exit in mouseOut
// Then start (in parallel) and wait until the fadeOut/slideOut complete
from fadeOutComplete in fadeOut.BeginObservable()
from slideOutComplete in slideOut.BeginObservable()
where fadeOutComplete && slideOutComplete
// And finally signal that this sequence is done
// (we gotta select something, but we don't care what,
// so we'll select the equivalent of "nothing" in Rx speak)
select new Unit();
EDIT EDIT: Here's the full Test rig I used, maybe you can port to your needs:
void Main()
{
var window = new Window();
var control = new MyControl();
window.Content = control;
window.Show();
}
public class MyControl : UserControl
{
protected DockPanel _root;
protected Rectangle _toaster;
protected CompositeDisposable _disposables;
protected Thickness _defaultToasterPosition = new Thickness(10, -60, 10, 10);
public MyControl()
{
this.InitializeComponent();
}
protected void InitializeComponent()
{
_disposables = new CompositeDisposable();
_root = new DockPanel();
_toaster = new Rectangle();
_toaster.SetValue(Rectangle.NameProperty, "toaster1");
_toaster.Fill = Brushes.Red;
_toaster.Stroke = Brushes.Black;
_toaster.StrokeThickness = 3;
_toaster.Width = 50;
_toaster.Height = 50;
_toaster.Opacity = 0.1;
DockPanel.SetDock(_toaster, Dock.Bottom);
_toaster.Margin = _defaultToasterPosition;
rxPop();
_root.Children.Add(_toaster);
this.Content = _root;
this.Unloaded += (o,e) =>
{
if(_disposables != null) _disposables.Dispose();
};
}
private void rxPop()
{
var toaster = (Rectangle)this.FindName("toaster1") ?? _toaster;
var defaultToasterPosition = toaster.Margin;
var newToasterPosition = new Thickness(
defaultToasterPosition.Left, defaultToasterPosition.Top,
defaultToasterPosition.Right, defaultToasterPosition.Bottom + toaster.Height);
/* Animations */
var slideIn = slide(toaster, newToasterPosition, TimeSpan.FromMilliseconds(450));
var slideOut = slide(toaster, defaultToasterPosition, TimeSpan.FromMilliseconds(450));
var fadeIn = animateOpacity(toaster, 1.0, TimeSpan.FromMilliseconds(150));
var fadeOut = animateOpacity(toaster, 0.1, TimeSpan.FromSeconds(3));
/* Events */
var mouseEnter = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseEnter += h,
h => toaster.MouseEnter -= h);
var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseLeave += h,
h => toaster.MouseLeave -= h);
var showToast =
// Start this mess on a "mouse enter" event
from enter in mouseEnter
// Start (in parallel) and wait until the fadeIn/slideIn complete
from fadeInComplete in fadeIn.BeginObservable()
from slideInComplete in slideIn.BeginObservable()
where fadeInComplete && slideInComplete
// Until you see a "mouse out" event
from exit in mouseOut
// Then start (in parallel) and wait until the fadeOut/slideOut complete
from fadeOutComplete in fadeOut.BeginObservable()
from slideOutComplete in slideOut.BeginObservable()
where fadeOutComplete && slideOutComplete
// And finally signal that this sequence is done
// (we gotta select something, but we don't care what,
// so we'll select the equivalent of "nothing" in Rx speak)
select new Unit();
_disposables.Add(showToast.Subscribe());
}
private Storyboard slide(Rectangle rect, Thickness newPosition, TimeSpan inTime)
{
var sb = new Storyboard();
sb.Duration = inTime;
Storyboard.SetTarget(sb, rect);
Storyboard.SetTargetProperty(sb, new PropertyPath(Rectangle.MarginProperty));
var path = new ThicknessAnimation(newPosition, inTime);
sb.Children.Add(path);
return sb;
}
private Storyboard animateOpacity(Rectangle rect, double newOpacity, TimeSpan inTime)
{
var sb = new Storyboard();
sb.Duration = inTime;
Storyboard.SetTarget(sb, rect);
Storyboard.SetTargetProperty(sb, new PropertyPath(Rectangle.OpacityProperty));
var path = new DoubleAnimation(newOpacity, inTime);
sb.Children.Add(path);
return sb;
}
}
public static class Ext
{
public static IObservable<bool> BeginObservable(this Storyboard animation)
{
var sub = new BehaviorSubject<bool>(false);
var onComplete = Observable.FromEventPattern<EventHandler, EventArgs>(
h => animation.Completed += h,
h => animation.Completed -= h);
IDisposable subscription = null;
subscription = onComplete.Subscribe(e =>
{
Console.WriteLine("Animation {0} complete!", animation.Name);
sub.OnNext(true);
if(subscription != null)
subscription.Dispose();
});
Console.WriteLine("Starting animation {0}...", animation.Name);
animation.Begin();
return sub;
}
}

When to start the WPF Progress Bar

I want my app to show a running progress bar while doing some components checking. However, due to my lack of knowledge in Desktop app programming and WPF, I cannot find suitable place for it.
I tried to show the incrementing the progress Bar during the Window_Loaded(), ContentRendered() but with no luck.
Instead of showing the progressBar increases, it just show the final state of the progress Bar.
Here is the code
public partial class Loading : Window
{
public Loading()
{
InitializeComponent();
SetProgressBar();
this.Show();
CheckComponents();
}
private void CheckComponents()
{
System.Threading.Thread.Sleep(3000);
CheckProductionDBConnection();
pgrsBar.Value = 30;
System.Threading.Thread.Sleep(3000);
CheckInternalDBConnection();
pgrsBar.Value = 60;
System.Threading.Thread.Sleep(3000);
CheckProductionPlanning();
pgrsBar.Value = 90;
//MainWindow mainWindow = new MainWindow();
//mainWindow.Show();
}
private void SetProgressBar()
{
pgrsBar.Minimum = 0;
pgrsBar.Maximum = 100;
pgrsBar.Value = 0;
}
//more code down here...
Where should I put the CheckComponents() method?
You could put this code in an event handler subscribed to the Activated event. The one catch with this is that the Activated event is fired every time the window receives focus after having lost it. To get around this, the first thing you can do in your event handler is unsubscribe from the Activated event so that your code is executed only the first time the window is activated.
You also need to offload this work to a worker thread if you don't want the delay to block the main thread. If you do that, you'll have to invoke your calls to update the progess bar's value.
Here's some sample code to get you started:
public Loader()
{
InitializeComponent();
SetProgressBar();
this.Activated += OnActivatedFirstTime;
}
private void OnActivatedFirstTime(object sender, EventArgs e)
{
this.Activated -= this.OnActivatedFirstTime;
ThreadPool.QueueUserWorkItem(x =>
{
System.Threading.Thread.Sleep(3000);
CheckProductionDBConnection();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 30));
System.Threading.Thread.Sleep(3000);
CheckInternalDBConnection();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 60));
System.Threading.Thread.Sleep(3000);
CheckProductionPlanning();
this.Dispatcher.BeginInvoke(new Action(() => pgrsBar.Value = 90));
});
}
private void SetProgressBar()
{
pgrsBar.Minimum = 0;
pgrsBar.Maximum = 100;
pgrsBar.Value = 0;
}

How to Raise DragDelta event on Thumb

As you can see below, I want to start moving when the component visibility changes.
because otherwise I need the user to click again to start the movement, and that is bad in terms of usability for my application.
public MoveILayoutControl()
{
InitializeComponent();
this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(MoveILayoutControl_IsVisibleChanged);
this.moveThumb.DragDelta += new DragDeltaEventHandler(MoveThumb_DragDelta);
}
void MoveILayoutControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.IsVisible)
{
// Raise Drag Event !?
}
}
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var myData = DataContext as ILayoutVisual;
if (myData != null)
{
Point dragDelta = new Point(e.HorizontalChange, e.VerticalChange);
if (myData.Rotation != 0)
{
Matrix toCalculate = ((this.Parent as FrameworkElement).RenderTransform).Value;
if (toCalculate != null)
{
dragDelta = toCalculate.Transform(dragDelta);
}
}
myData.X += dragDelta.X;
myData.Y += dragDelta.Y;
}
}
I believe the only way is using reflection to change the internal values ​​of the thumb. Changing the property "IsDragging" (not tested).
I looked up the source code of Thumb and I think a better way is to simulate a MouseLeftButtonEvent on the thumb:
var evt = new MouseButtonEventArgs(mouseDevice, timestamp, MouseButton.Left)
{
RoutedEvent = UIElement.MouseLeftButtonDownEvent
};
thumb.RaiseEvent(evt);

WPF - sequential animation simple example

I'm learning about WPF animation, and am confused about how to apply animations sequentially. As a simple example, I've got four rectangles in a uniform grid, and would like to change the color of each one sequentially. Here's what I have so far:
public partial class Window1 : Window
{
Rectangle blueRect;
Rectangle redRect;
Rectangle greenRect;
Rectangle yellowRect;
public Window1()
{
InitializeComponent();
blueRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Blue, Name="Blue"};
redRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Red, Name="Yellow"};
greenRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Green, Name="Green" };
yellowRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Yellow, Name="Yellow" };
UniformGrid1.Children.Add(blueRect);
UniformGrid1.Children.Add(redRect);
UniformGrid1.Children.Add(greenRect);
UniformGrid1.Children.Add(yellowRect);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
animateCell(blueRect, Colors.Blue);
animateCell(redRect, Colors.Red);
}
private void animateCell(Rectangle rectangle, Color fromColor)
{
Color toColor = Colors.White;
ColorAnimation ani = new ColorAnimation(toColor, new Duration(TimeSpan.FromMilliseconds(300)));
ani.AutoReverse = true;
SolidColorBrush newBrush = new SolidColorBrush(fromColor);
ani.BeginTime = TimeSpan.FromSeconds(2);
rectangle.Fill = newBrush;
newBrush.BeginAnimation(SolidColorBrush.ColorProperty, ani);
//NameScope.GetNameScope(this).RegisterName(rectangle.Name, rectangle);
//Storyboard board = new Storyboard();
//board.Children.Add(ani);
//Storyboard.SetTargetName(rectangle, rectangle.Name);
//Storyboard.SetTargetProperty(ani, new PropertyPath(SolidColorBrush.ColorProperty));
//board.Begin();
}
What's the easiest way of accomplishing this? The code in the comments is my first guess, but it's not working correctly.
There should be an event ani.Completed - handle that event and start the next phase of the animation, then start the first one running and each phase will trigger the next.
ColorAnimation ani = // whatever...
ani.Completed += (s, e) =>
{
ColorAnimation ani2 = // another one...
// and so on
};
newBrush.BeginAnimation(SolidColorBrush.ColorProperty, ani);
UPDATE:
public partial class Window1 : Window
{
Rectangle blueRect;
Rectangle redRect;
Rectangle greenRect;
Rectangle yellowRect;
public Window1()
{
InitializeComponent();
blueRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Blue, Name = "Blue" };
redRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Red, Name = "Yellow" };
greenRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Green, Name = "Green" };
yellowRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Yellow, Name = "Yellow" };
UniformGrid1.Children.Add(blueRect);
UniformGrid1.Children.Add(redRect);
UniformGrid1.Children.Add(greenRect);
UniformGrid1.Children.Add(yellowRect);
}
IEnumerable<Action<Action>> AnimationSequence()
{
for (; ; )
{
yield return AnimateCell(blueRect, Colors.Blue);
yield return AnimateCell(redRect, Colors.Red);
yield return AnimateCell(greenRect, Colors.Green);
yield return AnimateCell(yellowRect, Colors.Yellow);
}
}
private IEnumerator<Action<Action>> _actions;
private void RunNextAction()
{
if (_actions.MoveNext())
_actions.Current(RunNextAction);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_actions = AnimationSequence().GetEnumerator();
RunNextAction();
}
private Action<Action> AnimateCell(Rectangle rectangle, Color fromColor)
{
return completed =>
{
Color toColor = Colors.White;
ColorAnimation ani = new ColorAnimation(toColor,
new Duration(TimeSpan.FromMilliseconds(300)));
ani.AutoReverse = true;
ani.Completed += (s, e) => completed();
SolidColorBrush newBrush = new SolidColorBrush(fromColor);
ani.BeginTime = TimeSpan.FromSeconds(2);
rectangle.Fill = newBrush;
newBrush.BeginAnimation(SolidColorBrush.ColorProperty, ani);
};
}
}
Try pasting the above into your program. It does what you need, but in a way that may be useful to you in other contexts. It's still event driven, but it uses an "iterator method" (with yield return) to create the impression that it is sequential coding that blocks while the animation is going on.
The nice thing about this is that you can play around with the AnimationSequence method in a very intuitive way - you could write out the timeline of the animation in a series of statements, or use loops, or whatever you want.
The solution I've tried is to use a Queue like so. This will let you add to the animation chain dynamically. I'm not sure if the lock is necessary, but I left it in just to be safe.
Queue<Object[]> animationQueue = new Queue<Object[]>();
void sequentialAnimation(DoubleAnimation da, Animatable a, DependencyProperty dp)
{
da.Completed += new EventHandler(da_Completed);
lock (animationQueue)
{
if (animationQueue.Count == 0) // no animation pending
{
animationQueue.Enqueue(new Object[] { da, a, dp });
a.BeginAnimation(dp, da);
}
else
{
animationQueue.Enqueue(new Object[] { da, a, dp });
}
}
}
void da_Completed(object sender, EventArgs e)
{
lock (animationQueue)
{
Object[] completed = animationQueue.Dequeue();
if (animationQueue.Count > 0)
{
Object[] next = animationQueue.Peek();
DoubleAnimation da = (DoubleAnimation)next[0];
Animatable a = (Animatable)next[1];
DependencyProperty dp = (DependencyProperty)next[2];
a.BeginAnimation(dp, da);
}
}
}
This can be accomplished by using a class with the contradictory name ParallelTimeline and carefully adjusting the BeginTime property. Note in the example below how the BeginTime property of the second DoubleAnimation is set to the duration of the first.
<ParallelTimeline>
<DoubleAnimation
Storyboard.TargetName="FlashRectangle"
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="0:0:1"/>
<DoubleAnimation BeginTime="0:0:0.05"
Storyboard.TargetName="FlashRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:2"/>
</ParallelTimeline>

Resources