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

XAF中WinWindow.Close()无效 如何通过代码正确关闭窗口

XAF Win窗口代码关闭失效问题处理

问题复现

尝试通过以下代码关闭指定XAF窗口时无效果:

public static bool CloseViewIfOpen(XafApplication application, string listViewId)
{
    if (!(application.ShowViewStrategy is WinShowViewStrategyBase strategy)) return false;
    foreach (var win in strategy.Windows.ToArray())
    {
        if (win.View == null) continue;
        if (!win.View.Id.Equals(listViewId)) continue;
        win.Close();
        return true;
    }
    return false;
}

排查源码可见DevExpress.ExpressApp.Win.WinWindow的无参Close()方法未执行实际关闭逻辑,直接返回false,对应源码截图如下:
WinWindow.Close方法源码
此前尝试直接调用win.Dispose()方法,但win.controllers集合始终存在,无法完成资源释放与窗口关闭。
当前使用XAF版本为21.2.8,用户可通过UI界面正常关闭目标窗口,需求为关闭窗口后重新构建其嵌套列表视图的集合数据源。

可用解决方案

  • 方案1:调用带参数的Close重载
    无参Close()方法默认不会触发完整关闭流程,传入布尔参数标记为用户触发的关闭操作即可走标准关闭逻辑,替换原有win.Close()调用即可:
    // 传入true表示用户发起的关闭,执行完整窗口关闭、资源回收流程
    win.Close(true);
    
  • 方案2:调用窗口包装的原生WinForm窗体Close方法
    WinWindow本质是对原生System.Windows.Forms.Form的包装,直接获取底层窗体实例调用Close方法,执行逻辑和用户点击窗口右上角关闭按钮完全一致:
    if(win is WinWindow targetWin)
    {
        Form targetForm = targetWin.Form;
        if(targetForm != null && !targetForm.IsDisposed)
        {
            targetForm.Close();
        }
    }
    

注意:不要直接调用Dispose()方法释放窗口,XAF窗口生命周期由ShowViewStrategy统一托管,直接调用Dispose会破坏内部引用计数,导致Controllers集合无法正常回收,出现内存残留。

  • 方案3:无需关闭窗口,直接刷新嵌套列表数据源
    如果只是为了重建嵌套列表视图的集合数据源,不需要关闭整个窗口,直接获取对应列表的CollectionSource调用刷新方法即可,性能更高:
    if(win.View is DetailView targetDetailView)
    {
        ListPropertyEditor nestedListEditor = targetDetailView.FindItem("目标嵌套列表的属性名/ID") as ListPropertyEditor;
        nestedListEditor?.ListView.CollectionSource.Refresh();
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 22:01:09