From 5ef6145688eb691994e6a2626c66951f2879f78a Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Tue, 20 Jan 2026 19:12:45 +0800
Subject: [PATCH] =?UTF-8?q?fix(ui):=20=E4=BF=AE=E5=A4=8DUiRouterBase?=
=?UTF-8?q?=E5=BC=B9=E5=87=BA=E6=A0=88=E6=97=B6=E7=9B=AE=E6=A0=87UI?=
=?UTF-8?q?=E9=94=AE=E5=8F=AF=E7=A9=BA=E6=80=A7=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将topUiKey重命名为leavingUiKey以提高代码可读性
- 修改nextUiKey逻辑使其在栈中只有一个元素时返回null而非抛出异常
- 更新CreateEvent方法参数toUiKey为可空字符串类型
- 调整UiTransitionEvent中的ToUiKey属性为可空字符串类型
- 添加注释说明nextUiKey现在是可选的
- 格式化代码缩进和换行以提高可读性
---
.../ui/UiTransitionEvent.cs | 2 +-
GFramework.Game/ui/UiRouterBase.cs | 41 +++++++++++--------
2 files changed, 24 insertions(+), 19 deletions(-)
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