Skip to content

Commit

Permalink
#506 Navigation and Forms adapter
Browse files Browse the repository at this point in the history
All async void temporarily till we move them across properly.
  • Loading branch information
Nigel Sampson committed Mar 5, 2018
1 parent 7f36022 commit babb62f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
10 changes: 10 additions & 0 deletions src/Caliburn.Micro.Core/ActivateExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace Caliburn.Micro
{
public static class ActivateExtensions
{
public static Task ActivateAsync(this IActivate activate) => activate.ActivateAsync(CancellationToken.None);
}
}
23 changes: 11 additions & 12 deletions src/Caliburn.Micro.Platform/Platforms/UWP/INavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Caliburn.Micro {
using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;


/// <summary>
/// Implemented by services that provide (<see cref="System.Uri" /> based) navigation.
Expand Down Expand Up @@ -221,7 +221,7 @@ protected virtual void OnNavigated(object sender, NavigationEventArgs e) {
/// </summary>
/// <param name="view">The view.</param>
/// <param name="viewModel">The view model.</param>
protected virtual void BindViewModel(DependencyObject view, object viewModel = null)
protected virtual async void BindViewModel(DependencyObject view, object viewModel = null)
{
ViewLocator.InitializeComponent(view);

Expand All @@ -238,10 +238,9 @@ protected virtual void BindViewModel(DependencyObject view, object viewModel = n
TryInjectParameters(viewModel, CurrentParameter);
ViewModelBinder.Bind(viewModel, view, null);

var activator = viewModel as IActivate;
if (activator != null)
if (viewModel is IActivate activator)
{
activator.Activate();
await activator.ActivateAsync();
}
}

Expand Down
36 changes: 20 additions & 16 deletions src/Caliburn.Micro.Platform/Platforms/WPF/INavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace Caliburn.Micro
{
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace Caliburn.Micro
{
/// <summary>
/// Implemented by services that provide <see cref="Uri" /> based navigation.
/// </summary>
public interface INavigationService {
public interface INavigationService
{
/// <summary>
/// Navigates to the view represented by the given view model.
/// </summary>
Expand Down Expand Up @@ -133,7 +133,11 @@ protected virtual void OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var shouldCancel = false;
var runningAsync = true;
guard.CanClose(result => { runningAsync = false; shouldCancel = !result; });
guard.CanClose(result =>
{
runningAsync = false;
shouldCancel = !result;
});
if (runningAsync)
throw new NotSupportedException("Async CanClose is not supported.");

Expand Down Expand Up @@ -169,7 +173,7 @@ protected virtual bool CanCloseOnNavigating(object sender, NavigatingCancelEvent
/// </summary>
/// <param name="sender"> The event sender. </param>
/// <param name="e"> The event args. </param>
protected virtual void OnNavigated(object sender, NavigationEventArgs e)
protected virtual async void OnNavigated(object sender, NavigationEventArgs e)
{
if (e.Uri.IsAbsoluteUri || e.Content == null)
{
Expand Down Expand Up @@ -198,10 +202,9 @@ protected virtual void OnNavigated(object sender, NavigationEventArgs e)
TryInjectParameters(viewModel, e.ExtraData);
ViewModelBinder.Bind(viewModel, page, null);

var activator = viewModel as IActivate;
if (activator != null)
if (viewModel is IActivate activator)
{
activator.Activate();
await activator.ActivateAsync();
}

GC.Collect();
Expand Down Expand Up @@ -330,7 +333,8 @@ public void NavigateToViewModel<TViewModel>(object extraData = null)
/// Removes the most recent entry from the back stack.
/// </summary>
/// <returns> The entry that was removed. </returns>
public JournalEntry RemoveBackEntry() {
public JournalEntry RemoveBackEntry()
{
return frame.RemoveBackEntry();
}

Expand Down Expand Up @@ -379,4 +383,4 @@ public event FragmentNavigationEventHandler FragmentNavigation
remove { frame.FragmentNavigation -= value; }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void DisplayRootView<T>()
/// Locates the view model, locates the associate view, binds them and shows it as the root view.
/// </summary>
/// <param name="viewModelType">The view model type.</param>
protected void DisplayRootViewFor(Type viewModelType)
protected async Task DisplayRootViewForAsync(Type viewModelType)
{
var viewModel = IoC.GetInstance(viewModelType, null);
var view = ViewLocator.LocateForModel(viewModel, null, null);
Expand All @@ -114,9 +114,10 @@ protected void DisplayRootViewFor(Type viewModelType)

ViewModelBinder.Bind(viewModel, view, null);

var activator = viewModel as IActivate;
if (activator != null)
activator.Activate();
if (viewModel is IActivate activator)
{
await activator.ActivateAsync();
}

MainPage = page;
}
Expand All @@ -125,9 +126,9 @@ protected void DisplayRootViewFor(Type viewModelType)
/// Locates the view model, locates the associate view, binds them and shows it as the root view.
/// </summary>
/// <typeparam name="T">The view model type.</typeparam>
protected void DisplayRootViewFor<T>()
protected Task DisplayRootViewForAsync<T>()
{
DisplayRootViewFor(typeof(T));
return DisplayRootViewForAsync(typeof(T));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ protected virtual void DeactivateView(BindableObject view)
/// Apply logic to activate a view when it is popped onto the navigation stack
/// </summary>
/// <param name="view">the view to activate</param>
protected virtual void ActivateView(BindableObject view)
protected virtual async void ActivateView(BindableObject view)
{
var activate = view?.BindingContext as IActivate;
activate?.Activate();
if (view?.BindingContext is IActivate activator)
{
await activator.ActivateAsync();
}
}

/// <summary>
Expand Down

0 comments on commit babb62f

Please sign in to comment.