Work on screens: tidying, bug-fixing

This commit is contained in:
Antony Male 2014-03-26 13:16:26 +00:00
parent abb1d340b8
commit 3e102ea274
5 changed files with 28 additions and 48 deletions

View File

@ -17,7 +17,7 @@ namespace Stylet
T ActiveItem { get; set; }
}
public interface IConductor<T> : IParent<T>
public interface IConductor<in T>
{
void ActivateItem(T item);

View File

@ -40,11 +40,6 @@ namespace Stylet
object Parent { get; set; }
}
public interface IDialogClose
{
void TryClose();
}
public interface IClose
{
void Close();
@ -56,7 +51,7 @@ namespace Stylet
Task<bool> CanCloseAsync();
}
public interface IScreen : IViewAware, IHaveDisplayName, IActivate, IDeactivate, IChild, IDialogClose, IClose, IGuardClose
public interface IScreen : IViewAware, IHaveDisplayName, IActivate, IDeactivate, IChild, IClose, IGuardClose
{
}

View File

@ -10,39 +10,13 @@ namespace Stylet
{
public class Screen : PropertyChangedBase, IScreen
{
public virtual void TryClose(bool? dialogResult = null)
{
// TODO: Check for parent conductor
var viewWindow = this.View as Window;
if (viewWindow != null)
{
if (dialogResult != null)
viewWindow.DialogResult = dialogResult;
viewWindow.Close();
return;
}
var viewPopover = this.View as Popup;
if (viewPopover != null)
{
viewPopover.IsOpen = false;
return;
}
throw new InvalidOperationException(String.Format("Unable to close ViewModel {0} as it must have a parent, or its view must be a Window", this.GetType().Name));
}
#region IHaveDisplayName
private string _displayName;
public string DisplayName
{
get { return this._displayName; }
set
{
this._displayName = value;
this.NotifyOfPropertyChange();
}
set { SetAndNotify(ref this._displayName, value); }
}
#endregion
@ -55,11 +29,7 @@ namespace Stylet
public bool IsActive
{
get { return this._isActive; }
set
{
this._isActive = value;
this.NotifyOfPropertyChange();
}
set { SetAndNotify(ref this._isActive, value); }
}
void IActivate.Activate()
@ -164,15 +134,6 @@ namespace Stylet
#endregion
#region IDialogClose
public void TryClose()
{
this.TryClose(null);
}
#endregion
#region IGuardClose
public virtual Task<bool> CanCloseAsync()
@ -181,5 +142,27 @@ namespace Stylet
}
#endregion
public virtual void TryClose(bool? dialogResult = null)
{
// Conductor is contravariant, so it's always an IConductor<Screen>, even if they created a Conductor<IScreen> or Conductor<object>
var conductor = this.Parent as IConductor<Screen>;
if (conductor != null)
{
conductor.CloseItem(this);
return;
}
var viewWindow = this.View as Window;
if (viewWindow != null)
{
if (dialogResult != null)
viewWindow.DialogResult = dialogResult;
viewWindow.Close();
return;
}
throw new InvalidOperationException(String.Format("Unable to close ViewModel {0} as it must have a conductor as a parent, or its view must be a Window", this.GetType().Name));
}
}
}

View File

@ -110,6 +110,7 @@ namespace StyletUnitTests
public void RemovingItemRemovesParent()
{
var screen = new Mock<IScreen>();
screen.SetupGet(x => x.Parent).Returns(this.conductor);
this.conductor.Items.Add(screen.Object);
this.conductor.Items.Remove(screen.Object);
screen.VerifySet(x => x.Parent = null);

View File

@ -167,6 +167,7 @@ namespace StyletUnitTests
public void RemovingItemRemovesParent()
{
var screen = new Mock<IScreen>();
screen.SetupGet(x => x.Parent).Returns(this.conductor);
this.conductor.Items.Add(screen.Object);
this.conductor.Items.Remove(screen.Object);
screen.VerifySet(x => x.Parent = null);