diff --git a/GFramework.Game.Abstractions/ui/UiTransitionEvent.cs b/GFramework.Game.Abstractions/ui/UiTransitionEvent.cs index 7aa9bac..d29969d 100644 --- a/GFramework.Game.Abstractions/ui/UiTransitionEvent.cs +++ b/GFramework.Game.Abstractions/ui/UiTransitionEvent.cs @@ -22,7 +22,7 @@ public sealed class UiTransitionEvent /// /// 目标UI的标识符,切换后的UI key /// - public string ToUiKey { get; init; } = string.Empty; + public string? ToUiKey { get; init; } = string.Empty; /// /// UI切换类型 diff --git a/GFramework.Game/ui/UiRouterBase.cs b/GFramework.Game/ui/UiRouterBase.cs index c3ab761..f95197b 100644 --- a/GFramework.Game/ui/UiRouterBase.cs +++ b/GFramework.Game/ui/UiRouterBase.cs @@ -14,6 +14,16 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter { private static readonly ILogger Log = LoggerFactoryResolver.Provider.CreateLogger("UiRouterBase"); + /// + /// 路由守卫列表 + /// + private readonly List _guards = new(); + + /// + /// 层级管理(非栈层级),用于Overlay、Modal、Toast等浮层 + /// + private readonly Dictionary> _layers = new(); + /// /// UI切换处理器管道 /// @@ -24,11 +34,6 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter /// private readonly Stack _stack = new(); - /// - /// 层级管理(非栈层级),用于Overlay、Modal、Toast等浮层 - /// - private readonly Dictionary> _layers = new(); - /// /// UI工厂实例,用于创建UI相关的对象 /// @@ -36,11 +41,6 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter private IUiRoot _uiRoot = null!; - /// - /// 路由守卫列表 - /// - private readonly List _guards = new(); - /// /// 注册UI切换处理器 /// @@ -143,19 +143,23 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter return; } - var topUiKey = _stack.Peek().Key; + var leavingUiKey = _stack.Peek().Key; - // 执行离开守卫 - if (!ExecuteLeaveGuardsAsync(topUiKey).GetAwaiter().GetResult()) + if (!ExecuteLeaveGuardsAsync(leavingUiKey).GetAwaiter().GetResult()) { - Log.Warn("Pop blocked by guard: {0}", topUiKey); + Log.Warn("Pop blocked by guard: {0}", leavingUiKey); return; } + // ⚠️ 注意:nextUiKey 现在是可选的 var nextUiKey = _stack.Count > 1 - ? _stack.ElementAt(1).Key // 使用 Key 而不是 View.GetType().Name - : throw new InvalidOperationException("Stack is empty"); - var @event = CreateEvent(nextUiKey, UiTransitionType.Pop); + ? _stack.ElementAt(1).Key + : null; + + var @event = CreateEvent( + nextUiKey, + type: UiTransitionType.Pop + ); BeforeChange(@event); @@ -164,6 +168,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter AfterChange(@event); } + /// /// 替换当前所有页面为新页面(基于uiKey) /// @@ -312,7 +317,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter /// 创建UI切换事件 /// private UiTransitionEvent CreateEvent( - string toUiKey, + string? toUiKey, UiTransitionType type, UiTransitionPolicy? policy = null, IUiPageEnterParam? param = null