Add overridable CanClose() method to Screen - meaning they don't have to use Tasks

This commit is contained in:
Antony Male 2014-12-02 12:13:26 +00:00
parent e29e3bf061
commit 77c6d347a5
2 changed files with 27 additions and 2 deletions

View File

@ -220,10 +220,20 @@ namespace Stylet
/// <summary>
/// Called when a conductor wants to know whether this screen can close.
/// </summary>
/// <remarks>Internally, this calls CanClose, and wraps the response in a Task</remarks>
/// <returns>A task returning true (can close) or false (can't close)</returns>
public virtual Task<bool> CanCloseAsync()
{
return Task.FromResult(true);
return Task.FromResult(this.CanClose());
}
/// <summary>
/// Synchronous alternative to CanClose
/// </summary>
/// <returns>True if this screen can close, or false otherwise</returns>
protected virtual bool CanClose()
{
return true;
}
#endregion

View File

@ -55,6 +55,12 @@ namespace StyletUnitTests
{
this.OnViewLoadedCalled = true;
}
public bool? CanCloseResult = null;
protected override bool CanClose()
{
return this.CanCloseResult == null ? base.CanClose() : this.CanCloseResult.Value;
}
}
private MyScreen screen;
@ -248,13 +254,22 @@ namespace StyletUnitTests
}
[Test]
public void CanCloseAsyncReturnsCompletedTrueTask()
public void CanCloseAsyncReturnsTrueByDefault()
{
var task = this.screen.CanCloseAsync();
Assert.IsTrue(task.IsCompleted);
Assert.IsTrue(task.Result);
}
[Test]
public void CanCloseAsyncReturnsResultOfCanClose()
{
this.screen.CanCloseResult = false;
var task = this.screen.CanCloseAsync();
Assert.IsTrue(task.IsCompleted);
Assert.IsFalse(task.Result);
}
[Test]
public void TryCloseThrowsIfParentIsNotIChildDelegate()
{