提交 b2bf48eb 编写于 作者: D Dan Siegel

fix: android back button

上级 734ff635
......@@ -29,7 +29,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Maui.Tests", "..\..\t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Core.Tests", "..\..\tests\Prism.Core.Tests\Prism.Core.Tests.csproj", "{E0F13AA9-8083-47CA-B10D-93C5285D1505}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Events", "..\..\src\Prism.Events\Prism.Events.csproj", "{5623CB62-59C1-49BC-BB16-4C5D63D82DAC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Events", "..\..\src\Prism.Events\Prism.Events.csproj", "{5623CB62-59C1-49BC-BB16-4C5D63D82DAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -95,6 +95,7 @@ Global
{EE6F0C99-61D1-4E2E-8185-FBA0D246D5C7} = {E91F80AA-3D61-4C28-B876-3EDFB5921E7D}
{F3D2DFDB-95FB-4CBB-A624-35EB6550854D} = {E91F80AA-3D61-4C28-B876-3EDFB5921E7D}
{E0F13AA9-8083-47CA-B10D-93C5285D1505} = {E91F80AA-3D61-4C28-B876-3EDFB5921E7D}
{5623CB62-59C1-49BC-BB16-4C5D63D82DAC} = {8202B92A-A573-4365-8A15-E246504A7CBD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {50B0D1F3-D832-4C6C-858E-24F5F3B33632}
......
......@@ -269,13 +269,42 @@ public static class MvvmHelpers
if (lastModal != null)
page = lastModal;
return GetOnNavigatedToTargetFromChild(page);
return EvaluateCurrentPage(page);
};
private static Page EvaluateCurrentPage(Page target)
{
Page child = null;
if (target is FlyoutPage flyout)
child = flyout.Detail;
else if (target is TabbedPage tabbed)
child = tabbed.CurrentPage;
else if (target is NavigationPage np)
child = np.Navigation.NavigationStack.Last();
if (child != null)
target = GetOnNavigatedToTargetFromChild(child);
if (target is Page page)
return page.Parent switch
{
TabbedPage tab when tab.CurrentPage != target => EvaluateCurrentPage(tab.CurrentPage),
NavigationPage nav when nav.CurrentPage != target => EvaluateCurrentPage(nav.CurrentPage),
_ => target
};
return null;
}
public static async Task HandleNavigationPageGoBack(NavigationPage navigationPage)
{
var navigationService = Navigation.Xaml.Navigation.GetNavigationService(navigationPage.CurrentPage);
await navigationService.GoBackAsync();
var result = await navigationService.GoBackAsync();
if(result.Exception is NavigationException navEx && navEx.Message == NavigationException.CannotPopApplicationMainPage)
{
Application.Current.Quit();
}
}
public static void HandleSystemGoBack(IView previousPage, IView currentPage)
......
......@@ -15,8 +15,11 @@ public class PrismNavigationPage : NavigationPage
BackButtonPressed += HandleBackButtonPressed;
}
/// <inheritdoc/>
public event EventHandler BackButtonPressed;
/// <inheritdoc/>
protected override bool OnBackButtonPressed()
{
BackButtonPressed.Invoke(this, EventArgs.Empty);
......@@ -27,4 +30,4 @@ public class PrismNavigationPage : NavigationPage
{
await MvvmHelpers.HandleNavigationPageGoBack(this);
}
}
\ No newline at end of file
}
using Prism.AppModel;
using System.ComponentModel;
using Prism.AppModel;
using Prism.Common;
using Prism.Ioc;
using Prism.Navigation.Xaml;
using Prism.Services;
using Prism.Xaml;
using TabbedPage = Microsoft.Maui.Controls.TabbedPage;
namespace Prism.Navigation;
......@@ -19,6 +25,56 @@ internal class PrismWindow : Window
internal Page CurrentPage => Page is null ? null : MvvmHelpers.GetCurrentPage(Page);
internal bool IsRootPage => Page switch
{
TabbedPage tabbed => tabbed.CurrentPage,
NavigationPage nav => nav.RootPage,
_ => Page
} == CurrentPage;
[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
{
var navigation = container.Resolve<INavigationService>();
navigation.GoBackAsync();
}
}
private bool IsRoot(Page page)
{
if (page == Page) return true;
return page.Parent switch
{
FlyoutPage flyout => IsRoot(flyout),
TabbedPage tabbed => IsRoot(tabbed),
NavigationPage navigation => IsRoot(navigation),
_ => false
};
}
private async void PrismWindow_ModalPopping(object sender, ModalPoppingEventArgs e)
{
if (PageNavigationService.NavigationSource == PageNavigationSource.Device)
......
......@@ -45,28 +45,23 @@ public sealed class PrismAppBuilder
{
var root = ContainerLocator.Container;
if (root is null)
return true;
return false;
var app = root.Resolve<IApplication>();
var windows = app.Windows.OfType<PrismWindow>();
if (!windows.Any(x => x.IsActive))
return true;
return false;
var window = windows.First(x => x.IsActive);
var currentPage = window.CurrentPage;
var container = currentPage.GetContainerProvider();
if(currentPage is IDialogContainer dialogContainer)
if(window.IsRootPage && app is Application application)
{
if (dialogContainer.Dismiss.CanExecute(null))
dialogContainer.Dismiss.Execute(null);
}
else
{
var navigation = container.Resolve<INavigationService>();
navigation.GoBackAsync();
application.Quit();
return false;
}
return false;
window.OnSystemBack();
return true;
});
});
#endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册