Skip to content

Instantly share code, notes, and snippets.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
</PropertyGroup>
</Project>
public partial class App : Application
{
public App()
{
InitializeComponent();
var mainPage = new NavigationPage(new MainPage());
MainPage = mainPage;
}
// ....
public interface INavigationService
{
string CurrentPageKey { get; }
void Configure(string pageKey, Type pageType);
Task GoBack();
Task NavigateModalAsync(string pageKey, bool animated = true);
Task NavigateModalAsync(string pageKey, object parameter, bool animated = true);
Task NavigateAsync(string pageKey, bool animated = true);
Task NavigateAsync(string pageKey, object parameter, bool animated = true);
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)
{
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");
await _navigationService.NavigateAsync("PushNavigationPage", "simple string parameter");
<?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"/>
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;
private void Logout()
{
_navigationService.NavigateModalAsync(PageNames.LoginPage);
}
protected override bool OnBackButtonPressed()
{
return true;
}