Skip to content

Instantly share code, notes, and snippets.

View AwareNavigationController.cs
public class AwareNavigationController : UINavigationController
{
public event EventHandler PoppedViewController;
public AwareNavigationController():base() {}
public AwareNavigationController(UIViewController rootViewController):base(rootViewController){}
public AwareNavigationController(IntPtr intPtr) : base(intPtr) { }
public AwareNavigationController(NSCoder coder) : base(coder) { }
public AwareNavigationController(NSObjectFlag t) : base(t) { }
public AwareNavigationController(string nibName, NSBundle bundle) : base(nibName, bundle ) { }
View OnCompleted.cs
public LoginPage ()
{
InitializeComponent ();
BindingContext = ViewModel;
UsernameEntry.Completed += (sender, args) => { PasswordEntry.Focus(); };
PasswordEntry.Completed += (sender, args) => { ViewModel.AuthenticateCommand.Execute(null); };
}
View OnAppearing.cs
protected override void OnAppearing()
{
if (!App.IsUserLoggedIn)
{
App.NavigationService.NavigateModalAsync(PageNames.LoginPage, false);
}
base.OnAppearing();
}
View OnBackButtonPressed.cs
protected override bool OnBackButtonPressed()
{
return true;
}
View LogoutMethod.cs
private void Logout()
{
_navigationService.NavigateModalAsync(PageNames.LoginPage);
}
View LoginViewModel.cs
using System.ComponentModel;
using System.Windows.Input;
using LoginViewSample.Core.Services;
using Xamarin.Forms;
namespace LoginViewSample.Core.ViewModels
{
public class LoginViewModel : INotifyPropertyChanged
{
private string _username;
View LoginView.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="LoginViewSample.Core.Views.LoginPage">
<ContentPage.Content>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
View SampleNavigation.cs
await _navigationService.NavigateAsync("PushNavigationPage", "simple string parameter");
View App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
NavigationService.Configure("MainPage", typeof(Views.MainPage));
NavigationService.Configure("ModalNavigationPage", typeof(Views.ModalNavigationPage));
NavigationService.Configure("PushNavigationPage", typeof(Views.PushNavigationPage));
var mainPage = ((ViewNavigationService) NavigationService).SetRootPage("MainPage");
View NavigationService.cs
public class ViewNavigationService : INavigationService
{
private readonly object _sync = new object();
private readonly Dictionary<string, Type> _pagesByKey = new Dictionary<string, Type>();
private readonly Stack<NavigationPage> _navigationPageStack =
new Stack<NavigationPage>();
private NavigationPage CurrentNavigationPage => _navigationPageStack.Peek();
public void Configure(string pageKey, Type pageType)
{