.NETCore-winform 判断是否设计模式
在 .NETFramework 中 winform项目想要获得当前窗体是否处于设计器模式可以使用
public class CheckDesingModel { public static bool IsDesingMode() { bool returnFlag = false; #if DEBUG if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { returnFlag = true; } else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") { returnFlag = true; } #endif return returnFlag; } }
GarsonZhang www.yesdotnet.com
但是 这个方法如果在 .NETCore 或 .NET5 中使用就会报错:.NET Core Winform 打开窗体设计器报错
报错信息如图:
在 .NETCore-winform 中,如果想要判断窗体是否处于设计模式,需要在 winform 窗体中添加如下代码,然后根据 isDesignTime 属性来判断是否处于设计器模式
#region protected bool isDesignTime { get; private set; } public override ISite Site { get => base.Site; set { base.Site = value; this.isDesignTime = true; } } private bool init = false; protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.isDesignTime || this.init) { return; } this.init = true; // Do dangerous stuff here } #endregion
GarsonZhang www.yesdotnet.com
参考资料: https://github.com/dotnet/winforms/issues/3300#issuecomment-739137368
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post YES开发框架