fix(ui): 修复UiRouterBase弹出栈时目标UI键可空性问题

- 将topUiKey重命名为leavingUiKey以提高代码可读性
- 修改nextUiKey逻辑使其在栈中只有一个元素时返回null而非抛出异常
- 更新CreateEvent方法参数toUiKey为可空字符串类型
- 调整UiTransitionEvent中的ToUiKey属性为可空字符串类型
- 添加注释说明nextUiKey现在是可选的
- 格式化代码缩进和换行以提高可读性
This commit is contained in:
GeWuYou 2026-01-20 19:12:45 +08:00
parent 039152438b
commit 5ef6145688
2 changed files with 24 additions and 19 deletions

View File

@ -22,7 +22,7 @@ public sealed class UiTransitionEvent
/// <summary>
/// 目标UI的标识符切换后的UI key
/// </summary>
public string ToUiKey { get; init; } = string.Empty;
public string? ToUiKey { get; init; } = string.Empty;
/// <summary>
/// UI切换类型

View File

@ -14,6 +14,16 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
{
private static readonly ILogger Log = LoggerFactoryResolver.Provider.CreateLogger("UiRouterBase");
/// <summary>
/// 路由守卫列表
/// </summary>
private readonly List<IUiRouteGuard> _guards = new();
/// <summary>
/// 层级管理非栈层级用于Overlay、Modal、Toast等浮层
/// </summary>
private readonly Dictionary<UiLayer, Dictionary<string, IUiPageBehavior>> _layers = new();
/// <summary>
/// UI切换处理器管道
/// </summary>
@ -24,11 +34,6 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
/// </summary>
private readonly Stack<IUiPageBehavior> _stack = new();
/// <summary>
/// 层级管理非栈层级用于Overlay、Modal、Toast等浮层
/// </summary>
private readonly Dictionary<UiLayer, Dictionary<string, IUiPageBehavior>> _layers = new();
/// <summary>
/// UI工厂实例用于创建UI相关的对象
/// </summary>
@ -36,11 +41,6 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
private IUiRoot _uiRoot = null!;
/// <summary>
/// 路由守卫列表
/// </summary>
private readonly List<IUiRouteGuard> _guards = new();
/// <summary>
/// 注册UI切换处理器
/// </summary>
@ -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);
}
/// <summary>
/// 替换当前所有页面为新页面基于uiKey
/// </summary>
@ -312,7 +317,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
/// 创建UI切换事件
/// </summary>
private UiTransitionEvent CreateEvent(
string toUiKey,
string? toUiKey,
UiTransitionType type,
UiTransitionPolicy? policy = null,
IUiPageEnterParam? param = null