Add OnStateChanged hook to Screen

This commit is contained in:
Antony Male 2015-02-27 16:48:04 +00:00
parent 21e6441a2f
commit 48ee25ba29
2 changed files with 28 additions and 0 deletions

View File

@ -111,6 +111,13 @@ namespace Stylet
/// </summary>
protected virtual void OnClose() { }
/// <summary>
/// Called on any state transition
/// </summary>
/// <param name="previousState">Previous state state</param>
/// <param name="newState">New state</param>
protected virtual void OnStateChanged(ScreenState previousState, ScreenState newState) { }
/// <summary>
/// Sets the screen's state to the given state, if it differs from the current state
/// </summary>
@ -126,6 +133,7 @@ namespace Stylet
this.logger.Info("Setting state from {0} to {1}", previousState, newState);
this.OnStateChanged(previousState, newState);
changedHandler(previousState, newState);
var handler = this.StateChanged;

View File

@ -55,6 +55,14 @@ namespace StyletUnitTests
this.OnCloseCalled = true;
}
public ScreenState PreviousState;
public ScreenState NewState;
protected override void OnStateChanged(ScreenState oldState, ScreenState newState)
{
this.PreviousState = oldState;
this.NewState = newState;
}
public bool OnViewLoadedCalled;
protected override void OnViewLoaded()
{
@ -152,6 +160,9 @@ namespace StyletUnitTests
Assert.AreEqual(ScreenState.Active, changedEventArgs[0].NewState);
Assert.AreEqual(ScreenState.Deactivated, changedEventArgs[0].PreviousState);
Assert.AreEqual(ScreenState.Deactivated, this.screen.PreviousState);
Assert.AreEqual(ScreenState.Active, this.screen.NewState);
Assert.AreEqual(1, activatedEventArgs.Count);
Assert.AreEqual(ScreenState.Deactivated, activatedEventArgs[0].PreviousState);
Assert.IsFalse(activatedEventArgs[0].IsInitialActivate);
@ -171,6 +182,9 @@ namespace StyletUnitTests
Assert.AreEqual(ScreenState.Active, changedEventArgs[0].NewState);
Assert.AreEqual(ScreenState.Initial, changedEventArgs[0].PreviousState);
Assert.AreEqual(ScreenState.Initial, this.screen.PreviousState);
Assert.AreEqual(ScreenState.Active, this.screen.NewState);
Assert.AreEqual(1, activatedEventArgs.Count);
Assert.AreEqual(ScreenState.Initial, activatedEventArgs[0].PreviousState);
Assert.IsTrue(activatedEventArgs[0].IsInitialActivate);
@ -228,6 +242,9 @@ namespace StyletUnitTests
Assert.AreEqual(ScreenState.Deactivated, changedEventArgs[0].NewState);
Assert.AreEqual(ScreenState.Active, changedEventArgs[0].PreviousState);
Assert.AreEqual(ScreenState.Active, this.screen.PreviousState);
Assert.AreEqual(ScreenState.Deactivated, this.screen.NewState);
Assert.AreEqual(1, deactivationEventArgs.Count);
Assert.AreEqual(ScreenState.Active, deactivationEventArgs[0].PreviousState);
}
@ -289,6 +306,9 @@ namespace StyletUnitTests
Assert.AreEqual(ScreenState.Closed, changedEventArgs[0].NewState);
Assert.AreEqual(ScreenState.Deactivated, changedEventArgs[0].PreviousState);
Assert.AreEqual(ScreenState.Deactivated, this.screen.PreviousState);
Assert.AreEqual(ScreenState.Closed, this.screen.NewState);
Assert.AreEqual(1, closeEventArgs.Count);
Assert.AreEqual(ScreenState.Deactivated, closeEventArgs[0].PreviousState);
}