Skip to content

Commit

Permalink
Merge pull request PrismLibrary#77 from almirvuk/feature/ConfirmNavig…
Browse files Browse the repository at this point in the history
…ation

ConfirmNavigation Sample implementation.
  • Loading branch information
dansiegel authored Jan 15, 2020
2 parents b7d8351 + 29a32eb commit 8cb5d62
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 49 deletions.
7 changes: 4 additions & 3 deletions 20-ConfirmNavigation/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Confirm Navigation

TODO
* **ViewAPage** is tab page, and it contains `Switch` and `Button` control, `Switch` is binding to `CanNavigateAway` property. `ViewAPageViewModel ` is implementing `IConfirmNavigation` interface and `CanNavigateAway` is used for returning value in `CanNavigate` method.

- Show use of IConfirmNavigation
- Show use of IConfirmNavigationAsync
* **ViewBPage** is tab page, and it contains `Button`, `Button` and `Command` property is binding to `NavigateAwayCommand`. `ViewBPageViewModel` is implementing `IConfirmNavigationAsync` interface and I am using `IPageDialogService` to ask user for confirm to navigate away, response from `DisplayAlertAsync` is used for return value of `CanNavigateAsync` method.

* **ViewCPage** is only used for navigating to it, and to show some kind of message with `Label` control. It also has `Button` to navigate back (GoBacAsync).
10 changes: 6 additions & 4 deletions 20-ConfirmNavigation/src/PrismSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Prism.Ioc;
using Prism.Ioc;
using PrismSample.ViewModels;
using PrismSample.Views;
using Xamarin.Forms;
Expand All @@ -16,7 +15,7 @@ protected override async void OnInitialized()
{
InitializeComponent();

var result = await NavigationService.NavigateAsync("MainPage");
var result = await NavigationService.NavigateAsync("TabbedPage?createTab=NonAsyncExamplePage&createTab=AsyncExamplePage");

if(!result.Success)
{
Expand All @@ -27,7 +26,10 @@ protected override async void OnInitialized()
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
containerRegistry.RegisterForNavigation<TabbedPage>();
containerRegistry.RegisterForNavigation<NonAsyncExamplePage, NonAsyncExamplePageViewModel>();
containerRegistry.RegisterForNavigation<AsyncExamplePage, AsyncExamplePageViewModel>();
containerRegistry.RegisterForNavigation<ResultPage, ResultPageViewModel>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using Prism.Navigation;
using Prism.Commands;
using Prism.Services;

namespace PrismSample.ViewModels
{
public class AsyncExamplePageViewModel : IConfirmNavigationAsync
{
private readonly INavigationService _navigationService;
private readonly IPageDialogService _pageDialogService;

public DelegateCommand NavigateAwayCommand { get; private set; }

public AsyncExamplePageViewModel(INavigationService navigationService,
IPageDialogService pageDialogService)
{
_navigationService = navigationService;
_pageDialogService = pageDialogService;

NavigateAwayCommand = new DelegateCommand(NavigateAway);
}

private async void NavigateAway()
{
await _navigationService.NavigateAsync("ResultPage");
}

public Task<bool> CanNavigateAsync(INavigationParameters parameters)
{
return _pageDialogService.DisplayAlertAsync(string.Empty, "Are you sure you want to navigate away?", "Yes", "No");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Prism.Navigation;
using Prism.Commands;
using Prism.Mvvm;

namespace PrismSample.ViewModels
{
public class NonAsyncExamplePageViewModel : BindableBase, IConfirmNavigation
{
private readonly INavigationService _navigationService;

private bool _canNavigateAway;
public bool CanNavigateAway
{
get { return _canNavigateAway; }
set { SetProperty(ref _canNavigateAway, value); }
}

public DelegateCommand NavigateAwayCommand { get; private set; }

public NonAsyncExamplePageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;

NavigateAwayCommand = new DelegateCommand(NavigateAway);
}

private async void NavigateAway()
{
await _navigationService.NavigateAsync("ResultPage");
}

public bool CanNavigate(INavigationParameters parameters)
{
return CanNavigateAway;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Prism.Navigation;
using Prism.Commands;

namespace PrismSample.ViewModels
{
public class ResultPageViewModel
{
private readonly INavigationService _navigationService;

public DelegateCommand GoBackCommand { get; private set; }

public ResultPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;

GoBackCommand = new DelegateCommand(GoBack);
}

private async void GoBack()
{
await _navigationService.GoBackAsync();
}
}
}
17 changes: 17 additions & 0 deletions 20-ConfirmNavigation/src/PrismSample/Views/AsyncExamplePage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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="PrismSample.Views.AsyncExamplePage"
Title="Async">
<StackLayout VerticalOptions="Center" Margin="16">

<Label Text="IConfirmNavigationAsync Example"
HorizontalOptions="Center"/>

<Button Text="Navigate away"
Command="{Binding NavigateAwayCommand}"
HorizontalOptions="Center"/>

</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xamarin.Forms;

namespace PrismSample.Views
{
public partial class AsyncExamplePage : ContentPage
{
public AsyncExamplePage()
{
InitializeComponent();
}
}
}
16 changes: 0 additions & 16 deletions 20-ConfirmNavigation/src/PrismSample/Views/MainPage.xaml

This file was deleted.

15 changes: 0 additions & 15 deletions 20-ConfirmNavigation/src/PrismSample/Views/MainPage.xaml.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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="PrismSample.Views.NonAsyncExamplePage"
Title="Non-Async">

<StackLayout VerticalOptions="Center"
Margin="16">

<Label Text="Can navigate?"
HorizontalOptions="Center"/>

<Switch IsToggled="{Binding CanNavigateAway}"
HorizontalOptions="Center"/>

<Button Text="Navigate away"
HorizontalOptions="Center"
Command="{Binding NavigateAwayCommand}"/>

</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xamarin.Forms;

namespace PrismSample.Views
{
public partial class NonAsyncExamplePage : ContentPage
{
public NonAsyncExamplePage()
{
InitializeComponent();
}
}
}
18 changes: 18 additions & 0 deletions 20-ConfirmNavigation/src/PrismSample/Views/ResultPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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="PrismSample.Views.ResultPage">

<StackLayout VerticalOptions="Center" Margin="16">

<Label Text="Congratulations! You did it!"
FontAttributes="Bold"
HorizontalOptions="Center"/>

<Button Text="Go back"
HorizontalOptions="Center"
Command="{Binding GoBackCommand}" />

</StackLayout>

</ContentPage>
12 changes: 12 additions & 0 deletions 20-ConfirmNavigation/src/PrismSample/Views/ResultPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xamarin.Forms;

namespace PrismSample.Views
{
public partial class ResultPage : ContentPage
{
public ResultPage()
{
InitializeComponent();
}
}
}

0 comments on commit 8cb5d62

Please sign in to comment.