You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Xamarin Forms中如何判断RgPlugn PopUp是否为空?

检查Rg.Plugins.Popup弹窗是否打开的方法

要避免空引用异常,你可以通过Rg.Plugins.Popup的导航栈判断是否有弹窗处于打开状态:

  • Rg.Plugins.Popup提供了PopupNavigation.Instance.PopupStack集合,该集合保存着当前所有已打开的弹窗页面
  • 判断PopupStack.Count > 0即可确认是否存在需要关闭的弹窗

修改你踢下线的代码如下:

private void RedirectAndInformInactivity()
{
    Device.BeginInvokeOnMainThread(async () =>
    {
        await Application.Current.MainPage.DisplayAlert("会话已结束!", "由于长时间未操作,您的会话将被终止!", "确定");
        // 先检查弹窗状态,再执行关闭操作
        if (PopupNavigation.Instance != null && PopupNavigation.Instance.PopupStack.Count > 0)
        {
            await PopupNavigation.Instance.PopAsync();
        }
        await Application.Current.MainPage.Navigation.PushAsync(new LogoutPage());
    });
}

额外说明:

  • 同时判断PopupNavigation.Instance != null是为了应对导航实例未初始化的极端场景
  • 若应用中可能同时打开多个弹窗,可改用PopupNavigation.Instance.PopAllAsync()一次性关闭所有弹窗

内容的提问来源于stack exchange,提问作者Mert Altiparmak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:57:01