Skip to content

Instantly share code, notes, and snippets.

@adamped
Created April 12, 2016 10:30
Show Gist options
  • Save adamped/c238db596d717050b196014075b611e8 to your computer and use it in GitHub Desktop.
Save adamped/c238db596d717050b196014075b611e8 to your computer and use it in GitHub Desktop.
public class NavigationContainer : INavigationContainer
{
private readonly NavigationPage _page = null;
public event EventHandler<IViewNavigationArgs> OnPopped;
private Queue<object> _argQueue = new Queue<object>();
private AsyncLock _lock = new AsyncLock();
public string CurrentViewKey { get; set; }
public NavigationContainer(NavigationPage page)
{
_page = page;
_page.Popped += _page_Popped;
}
private void _page_Popped(object sender, NavigationEventArgs e)
{
if (OnPopped != null)
{
var poppedPage = e.Page as IView;
var currentPage = _page.CurrentPage as IView;
var parameter = _argQueue.Count > 0 ? _argQueue.Dequeue() : null;
OnPopped(this, new ViewNavigationArgs() { Parameter = parameter, CurrentView = currentPage, PoppedView = poppedPage });
}
}
public void SetNavigationBar(bool isVisible, object page)
{
var bindableObject = page as BindableObject;
if (bindableObject != null)
NavigationPage.SetHasNavigationBar(bindableObject, isVisible);
}
public object View { get { return _page; } }
public bool CanGoBack()
{
return _page.Navigation.NavigationStack.Count > 1;
}
public async Task PopAsync(object parameter)
{
using (var releaser = await _lock.LockAsync())
{
_argQueue.Enqueue(parameter);
await _page.PopAsync();
}
}
public async Task PopAsync()
{
using (var releaser = await _lock.LockAsync())
{
await _page.PopAsync();
}
}
public async Task PushAsync(object page)
{
using (var releaser = await _lock.LockAsync())
{
await ThreadHelper.RunOnUIThreadAsync(async () =>
{
var xamarinPage = page as Page;
if (xamarinPage == null)
throw new Exception("PushAsync can not push a non Xamarin Page");
await _page.PushAsync(xamarinPage); // Must be run on the Main Thread
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment