/// <summary> /// Changes the active item. /// </summary> /// <param name="newItem">The new item to activate.</param> /// <param name="closePrevious">Indicates whether or not to close the previous active item.</param> /// <param name="cancellationToken"></param> protected virtual async Task ChangeActiveItem(T newItem, bool closePrevious, CancellationToken cancellationToken = default(CancellationToken)) { T previousActiveItem = activeItem; try { await ScreenExtensions.TryDeactivate(activeItem, closePrevious, cancellationToken); newItem = EnsureItem(newItem); activeItem = newItem; // this notification about ActiveItem causes the ViewModelBinder do it's work so the view is displayed // before OnInitialize/OnActivate are called NotifyOfPropertyChange("ActiveItem"); if (IsActive) { await ScreenExtensions.TryActivate(newItem, cancellationToken); } OnActivationProcessed(activeItem, true); } catch (OperationCanceledException) { Log.Info($"Canceling ChangeActiveItem {this}"); activeItem = previousActiveItem; throw; } }
/// <summary> /// Called when deactivating. /// </summary> /// <param name="close">Inidicates whether this instance will be closed.</param> protected override void OnDeactivate(bool close) { if (close) { items.OfType <IDeactivate>().Apply(x => x.Deactivate(true)); items.Clear(); } else { ScreenExtensions.TryDeactivate(ActiveItem, false); } }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> public override void ActivateItem(T item) { if (item != null && item.Equals(ActiveItem)) { if (IsActive) { ScreenExtensions.TryActivate(item); OnActivationProcessed(item, true); } return; } ChangeActiveItem(item, false); }
/// <summary> /// Called when deactivating. /// </summary> /// <param name="close">Indicates whether this instance will be closed.</param> /// <param name="cancellationToken"></param> protected override async Task OnDeactivate(bool close, CancellationToken cancellationToken = default(CancellationToken)) { if (close) { var deactivateables = items.OfType <IDeactivate>().Select(x => x.Deactivate(true, cancellationToken)); await Task.WhenAll(deactivateables); items.Clear(); } else { await ScreenExtensions.TryDeactivate(ActiveItem, false, cancellationToken); } }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> public override void ActivateItem(T item) { if (item == null) { return; } item = EnsureItem(item); if (IsActive) { ScreenExtensions.TryActivate(item); } OnActivationProcessed(item, true); }
/// <summary> /// Called when deactivating. /// </summary> /// <param name="close">Indicates whether this instance will be closed.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>A task that represents the asynchronous operation.</returns> protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken) { if (close) { foreach (var deactivate in _items.OfType <IDeactivate>()) { await deactivate.DeactivateAsync(true, cancellationToken); } _items.Clear(); } else { await ScreenExtensions.TryDeactivateAsync(ActiveItem, false, cancellationToken); } }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A task that represents the asynchronous operation.</returns> public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken = default) { if (item != null && item.Equals(ActiveItem)) { if (IsActive) { await ScreenExtensions.TryActivateAsync(item, cancellationToken); OnActivationProcessed(item, true); } return; } await ChangeActiveItemAsync(item, false, cancellationToken); }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>A task that represents the asynchronous operation.</returns> public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken) { if (item == null) { return; } item = EnsureItem(item); if (IsActive) { await ScreenExtensions.TryActivateAsync(item, cancellationToken); } OnActivationProcessed(item, true); }
private static Tuple <T, UIElement> CreateViewModelAndView <T>(Action <T> onInitialize = null) where T : IScreen { var vm = IoC.Get <T>(); var view = ViewLocator.LocateForModel(vm, null, null); ViewModelBinder.Bind(vm, view, null); if (onInitialize != null) { onInitialize(vm); } ScreenExtensions.TryActivate(vm); return(new Tuple <T, UIElement>(vm, view)); }
void CloseItemCore(T item) { if (item.Equals(ActiveItem)) { var index = items.IndexOf(item); var next = DetermineNextItemToActivate(items, index); ChangeActiveItem(next, true); } else { ScreenExtensions.TryDeactivate(item, true); } items.Remove(item); }
private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken = default) { if (item.Equals(ActiveItem)) { var index = _items.IndexOf(item); var next = DetermineNextItemToActivate(_items, index); await ChangeActiveItemAsync(next, true); } else { await ScreenExtensions.TryDeactivateAsync(item, true, cancellationToken); } _items.Remove(item); }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> /// <param name="cancellationToken"></param> public override async Task ActivateItem(T item, CancellationToken cancellationToken = default(CancellationToken)) { if (item != null && item.Equals(ActiveItem)) { if (IsActive) { cancellationToken.ThrowIfCancellationRequested(); await ScreenExtensions.TryActivate(item, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); OnActivationProcessed(item, true); } return; } cancellationToken.ThrowIfCancellationRequested(); await ChangeActiveItem(item, false, cancellationToken); }
/// <summary> /// Deactivates the specified item. /// </summary> /// <param name="item">The item to close.</param> /// <param name="close">Indicates whether or not to close the item after deactivating it.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>A task that represents the asynchronous operation.</returns> public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken = default) { if (item == null) { return; } if (!close) { await ScreenExtensions.TryDeactivateAsync(item, false, cancellationToken); } else { var closeResult = await CloseStrategy.ExecuteAsync(new[] { item }, CancellationToken.None); if (closeResult.CloseCanOccur) { await CloseItemCoreAsync(item, cancellationToken); } } }
/// <summary> /// Deactivates the specified item. /// </summary> /// <param name="item">The item to close.</param> /// <param name="close">Indicates whether or not to close the item after deactivating it.</param> /// <param name="cancellationToken"></param> public override async Task DeactivateItem(T item, bool close, CancellationToken cancellationToken = default(CancellationToken)) { if (item == null) { return; } if (!close) { await ScreenExtensions.TryDeactivate(item, false, cancellationToken); } else { var closeResult = await CloseStrategy.Execute(new[] { item }); if (closeResult.CanClose) { await CloseItemCore(item); } } }
/// <summary> /// Deactivates the specified item. /// </summary> /// <param name="item">The item to close.</param> /// <param name="close">Indicates whether or not to close the item after deactivating it.</param> public override void DeactivateItem(T item, bool close) { if (item == null) { return; } if (!close) { ScreenExtensions.TryDeactivate(item, false); } else { CloseStrategy.Execute(new[] { item }, (canClose, closable) => { if (canClose) { CloseItemCore(item); } }); } }
/// <summary> /// Activates the specified item. /// </summary> /// <param name="item">The item to activate.</param> public override void ActivateItem(T item) { if (item != null && item.Equals(ActiveItem)) { if (IsActive) { ScreenExtensions.TryActivate(item); OnActivationProcessed(item, true); } return; } CloseStrategy.Execute(new[] { ActiveItem }, (canClose, items) => { if (canClose) { ChangeActiveItem(item, true); } else { OnActivationProcessed(item, false); } }); }
/// <inheritdoc /> public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken = default) { if (item != null && item.Equals(ActiveItem)) { if (IsActive) { await ScreenExtensions.TryActivateAsync(item, cancellationToken); OnActivationProcessed(item, true); } return; } var closeResult = await CloseStrategy.ExecuteAsync(new[] { ActiveItem }, cancellationToken); if (closeResult.CloseCanOccur) { await ChangeActiveItemAsync(item, true, cancellationToken); } else { OnActivationProcessed(item, false); } }
private async Task CloseItemCore(T item, CancellationToken cancellationToken = default(CancellationToken)) { await ScreenExtensions.TryDeactivate(item, true, cancellationToken); items.Remove(item); }
/// <summary> /// Called when deactivating. /// </summary> /// <param name="close">Indicates whether this instance will be closed.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>A task that represents the asynchronous operation.</returns> protected override Task OnDeactivateAsync(bool close, CancellationToken cancellationToken) { return(ScreenExtensions.TryDeactivateAsync(ActiveItem, close, cancellationToken)); }
/// <summary> /// Called when activating. /// </summary> protected override void OnActivate() { ScreenExtensions.TryActivate(ActiveItem); }
private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken) { await ScreenExtensions.TryDeactivateAsync(item, true, cancellationToken); _items.Remove(item); }
/// <summary> /// Called when activating. /// </summary> /// <param name="cancellationToken"></param> protected override async Task OnActivate(CancellationToken cancellationToken = default(CancellationToken)) { await ScreenExtensions.TryActivate(ActiveItem, cancellationToken); }
/// <summary> /// Called when deactivating. /// </summary> /// <param name="close">Inidicates whether this instance will be closed.</param> protected override void OnDeactivate(bool close) { ScreenExtensions.TryDeactivate(ActiveItem, close); }
/// <summary> /// Called when activating. /// </summary> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>A task that represents the asynchronous operation.</returns> protected override Task OnActivateAsync(CancellationToken cancellationToken) { return(ScreenExtensions.TryActivateAsync(ActiveItem, cancellationToken)); }
void CloseItemCore(T item) { ScreenExtensions.TryDeactivate(item, true); items.Remove(item); }