|
public class NavigationService : INavigationService |
|
{ |
|
#region Fields |
|
|
|
private INavigation navigation => (Application.Current.MainPage as Main.Views.MainPage)?.Detail.Navigation; |
|
|
|
private readonly IDictionary<string, Type> pageLookup = new Dictionary<string, Type>(); |
|
|
|
#endregion |
|
|
|
#region Interface implementation |
|
|
|
public void RegisterPage(string pageKey, Type pageType) |
|
{ |
|
if (this.pageLookup.ContainsKey(pageKey)) |
|
{ |
|
throw new ArgumentException("That pagekey is already registered", pageKey); |
|
} |
|
|
|
this.pageLookup[pageKey] = pageType; |
|
} |
|
|
|
public void NavigateTo(string pageKey, bool animated = true) |
|
{ |
|
var pageType = this.pageLookup[pageKey] as Type; |
|
|
|
var page = ServiceLocator.Current.GetInstance(pageType) as Page; |
|
|
|
NavigateTo(page, animated); |
|
} |
|
|
|
public void NavigateTo(string pageKey, bool animated = true, params (string propertyName, object param)[] args) |
|
{ |
|
var pageType = this.pageLookup[pageKey] as Type; |
|
|
|
if (pageType == null) |
|
{ |
|
throw new ArgumentNullException(nameof(pageType)); |
|
} |
|
|
|
var parameters = new List<NamedParameter>(); |
|
|
|
foreach (var item in args) |
|
{ |
|
parameters.Add(new NamedParameter(item.propertyName, item.param)); |
|
} |
|
|
|
var page = App.Instance.Container.Resolve(pageType, parameters) as Page; |
|
|
|
if(page == null) |
|
{ |
|
throw new NullReferenceException("Page cannot be empty!"); |
|
} |
|
|
|
NavigateTo(page, animated); |
|
} |
|
|
|
public void GoBack() |
|
{ |
|
navigation.PopAsync(true).Start(); |
|
} |
|
|
|
#endregion |
|
|
|
#region Methods |
|
|
|
private void NavigateTo(Page page, bool animated = true) |
|
{ |
|
if (page == null) |
|
{ |
|
throw new ArgumentNullException(nameof(page)); |
|
} |
|
|
|
var last = navigation.NavigationStack.LastOrDefault(); |
|
if (last == null || last.GetType() != page.GetType()) |
|
{ |
|
Device.BeginInvokeOnMainThread(() => |
|
{ |
|
navigation.PushAsync(page, animated); |
|
}); |
|
} |
|
} |
|
|
|
#endregion |
|
} |