Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion e2e/Maui/MauiModule/ViewModels/ViewAViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
namespace MauiModule.ViewModels;

namespace MauiModule.ViewModels;

public class ViewAViewModel : ViewModelBase
{
public ViewAViewModel(BaseServices baseServices)
: base(baseServices)
{
}

public bool CanNavigateResult { get; set; }

public override bool CanNavigate(INavigationParameters parameters)
{
return CanNavigateResult;
}
}
2 changes: 1 addition & 1 deletion e2e/Maui/MauiModule/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void Initialize(INavigationParameters parameters)
Messages.Add(parameter.Value.ToString());
}

public void OnNavigatedFrom(INavigationParameters parameters)
public virtual void OnNavigatedFrom(INavigationParameters parameters)
{
Messages.Add("ViewModel NavigatedFrom");
}
Expand Down
8 changes: 7 additions & 1 deletion e2e/Maui/MauiModule/Views/ViewA.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:prism="http://prismlibrary.com"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Expand All @@ -8,6 +8,7 @@
BackgroundColor="White">
<Grid RowDefinitions="*,Auto"
ColumnDefinitions="*,*">

<CollectionView ItemsSource="{Binding Messages}"
Grid.ColumnSpan="2">
<CollectionView.Header>
Expand All @@ -19,6 +20,11 @@
<Label Text="Prism for .NET MAUI!"
HorizontalTextAlignment="Center"
Padding="0,0,0,20"/>
<StackLayout Orientation="Horizontal" >
<Label Text="Can Navigate Result:"/>
<CheckBox IsChecked="{Binding CanNavigateResult}" />
</StackLayout>

<Label Text="Navigation Events:"
FontAttributes="Bold"
Padding="10,0"/>
Expand Down
11 changes: 0 additions & 11 deletions e2e/Maui/PrismMauiDemo/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ public static MauiApp CreateMauiApp()
// .SelectedTab("NavigationPage|ViewB"))
// .AddParameter("message_global", "This is a Global Message")
// .Navigate())
//.CreateWindow("ViewA/ViewB") //broken
//.CreateWindow("NavigationPage/TabbedPage?createTab=ViewB/ViewC") //works
//.CreateWindow("ViewA/NavigationPage/TabbedPage?createTab=ViewB/ViewC") //works
//.CreateWindow(nav => nav
// .CreateBuilder()
// .AddTabbedSegment(page =>
// page.CreateTab(t =>
// t.AddNavigationPage()
// .AddSegment("ViewA")
// .AddSegment("ViewB")))
// .NavigateAsync()) //works
.CreateWindow(navigationService => navigationService.CreateBuilder()
.AddSegment<SplashPageViewModel>()
.NavigateAsync(HandleNavigationError))
Expand Down
4 changes: 2 additions & 2 deletions src/Maui/Prism.Maui/Common/MvvmHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static Task<bool> CanNavigateAsync(object page, INavigationParameters par
return implementer.CanNavigateAsync(parameters);
}

private static bool CanNavigate(object page, INavigationParameters parameters)
public static bool CanNavigate(object page, INavigationParameters parameters)
{
var implementer = GetImplementerFromViewOrViewModel<IConfirmNavigation>(page);
return implementer?.CanNavigate(parameters) ?? true;
Expand Down Expand Up @@ -288,7 +288,7 @@ public static void SetCurrentPageDelegate(Func<Page, Page?> getCurrentPageDelega
return EvaluateCurrentPage(page);
};

private static Page? GetTarget(Page? target)
internal static Page? GetTarget(Page? target)
{
return target switch
{
Expand Down
33 changes: 0 additions & 33 deletions src/Maui/Prism.Maui/Navigation/PrismWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,6 @@ private Page GetRootPage(Page page) =>
_ => page
};

/// <summary>
/// Handles the system back button press.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void OnSystemBack()
{
var currentPage = CurrentPage;
if (currentPage?.Parent is NavigationPage navPage)
{
// The NavigationPage has already taken care of the GoBack
return;
}

var container = currentPage.GetContainerProvider();

if (IsRoot(currentPage))
{
var app = container.Resolve<IApplication>() as Application;
app.Quit();
return;
}
else if (currentPage is IDialogContainer dialogContainer)
{
if (dialogContainer.Dismiss.CanExecute(null))
dialogContainer.Dismiss.Execute(null);
}
else if (PageNavigationService.NavigationSource == PageNavigationSource.Device)
{
var navigation = container.Resolve<INavigationService>();
navigation.GoBackAsync();
}
}

internal bool IsRoot(Page page)
{
if (page == Page) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Extensions.Logging;
using Prism.Xaml;
Expand Down Expand Up @@ -67,7 +67,9 @@ protected virtual void Log(Exception ex, INavigationParameters parameters)
protected void AddKnownNavigationParameters(INavigationParameters parameters)
{
parameters.Add(KnownNavigationParameters.Animated, Animated);
parameters.Add(KnownNavigationParameters.UseModalNavigation, UseModalNavigation);

if (UseModalNavigation.HasValue)
parameters.Add(KnownNavigationParameters.UseModalNavigation, UseModalNavigation);
}

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
Expand All @@ -76,4 +78,4 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
if (propertyName == nameof(Page) || propertyName == nameof(IsNavigating))
RaiseCanExecuteChanged();
}
}
}
32 changes: 20 additions & 12 deletions src/Maui/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ internal PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder
{
android.OnBackPressed(activity =>
{
var dialogModal = IDialogContainer.DialogStack.LastOrDefault();
if (dialogModal is not null)
{
if (dialogModal.Dismiss.CanExecute(null))
dialogModal.Dismiss.Execute(null);

return true;
}
//the PrismWindow and PrismNavigationPage have their own back press logic and intercepts the hardware back button behavior
//when this happens the PageNavigationService will take over and handle the navigation and decides whether to allow GoBack or not
//this means we need to check if the PageNavigationService is handling the navigation and if it is, we need to prevent the OnBackPressed logic
if (PageNavigationService.NavigationSource == PageNavigationSource.NavigationService)
return true;

var root = ContainerLocator.Container;
if (root is null)
Expand All @@ -78,13 +75,24 @@ internal PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder
if (window is null)
return false;

if(window.IsRootPage)
//we are on the root page and the user pressed the hardward back button. the app has nowhere to navigation except to the background.
//neither the PrismNavigationPage or the PrismWindow can handle this scenario, so we need to handle it here
if (window.IsRootPage)
{
return false;
//when showing a dialog on the root page, if the user presses the hardware back button, we need to make sure the dialog
//decides either to dismiss the dialog or keep it open
var dialogModal = IDialogContainer.DialogStack.LastOrDefault();
if (dialogModal is not null)
{
return true;
}

//note: if the PageNavigationService sends the android app to the background, this can cause the CanNavigate to be called twice.
//if this becomes a problem, we may need to add an additional static flag to know when we are sending the app to the background to prevent the double call
var canNavigate = MvvmHelpers.CanNavigate(MvvmHelpers.GetTarget(window.Page), new NavigationParameters());
return !canNavigate;
}

window.OnSystemBack();

return true;
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls;
using Moq;
using Prism.Common;
Expand Down Expand Up @@ -37,8 +37,6 @@ public void Execute_GoBackTypeDefault_GoesBackAPage()
}

[Theory]
[InlineData(true, null)]
[InlineData(false, null)]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls;
using Moq;
using Prism.Common;
using Prism.Maui.Tests.Mocks.Ioc;
Expand Down Expand Up @@ -57,8 +57,6 @@ public void Execute_NameIsSet_NavigatesToPage()
}

[Theory]
[InlineData(true, null)]
[InlineData(false, null)]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
Expand Down