.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开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:.NET Core Winform 打开窗体设计器报错
下一篇:.NETCore 运行项目报错:Could not load file or assembly ***
评论列表

发表评论

评论内容
昵称:
关联文章

.NETCore-winform 判断是否设计模式
.NETCore和.NET5 MVC 控制器中判断是否登录
C# winform程序判断是否管理员运行
wifnorm处于设计模式校验 (.NET6)
jquery判断checkbox是否选中
.NET Core Winform 打开窗体设计器报错
C#判断类型是否可空
C# .net WEB判断当前环境是否是调试
【UWP】开发小技巧――判断是否存在
GZDBHelper中HasRow方法,执行SQL语句,判断是否有返回数据
Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
浅析.netcore中的Configuration
.NETCore和.NET5 MVC使用 Session
.net中使用Linq 判断两个集合是否有交集的集合类
.net core winform窗体继承后设计器异常,看不到控件,并且页无法添加控件
数据绑定模式
YESWin Winform开发框架 Form窗体继承关系
YESWin Winform开发框架 代码生成器使用
Winform开发的快速、健壮、解耦的几点建议
.NETCore动态解析Razor代码cshtml代码解析RazorEngine.NetCore

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码