带嵌套控件的DesignMode [英] DesignMode with nested Controls

查看:69
本文介绍了带嵌套控件的DesignMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在开发控件时找到了解决DesignMode问题的有用方法吗?

Has anyone found a useful solution to the DesignMode problem when developing controls?

问题是,如果嵌套控件,则DesignMode仅适用于第一级。第二个及更低级别的DesignMode将始终返回FALSE。

The issue is that if you nest controls then DesignMode only works for the first level. The second and lower levels DesignMode will always return FALSE.

标准技巧是查看正在运行的进程的名称,如果它是 DevEnv.EXE 那么它一定是工作室,所以DesignMode确实是TRUE。

The standard hack has been to look at the name of the process that is running and if it is "DevEnv.EXE" then it must be studio thus DesignMode is really TRUE.

问题在于寻找ProcessName最终通过注册表和其他奇怪的部分工作导致用户可能没有查看进程名称所需的权限。此外,这条奇怪的路线非常慢。因此,我们不得不使用其他方法来使用单例,并且如果在询问进程名称时抛出错误,则假定DesignMode为FALSE。

The problem with that is looking for the ProcessName works its way around through the registry and other strange parts with the end result that the user might not have the required rights to see the process name. In addition this strange route is very slow. So we have had to pile additional hacks to use a singleton and if an error is thrown when asking for the process name then assume that DesignMode is FALSE.

一种好方法确定DesignMode是否正确。

A nice clean way to determine DesignMode is in order. Acually getting Microsoft to fix it internally to the framework would be even better!

推荐答案

重新审视这个问题,我现在已经发现了。 5种不同的方法,如下所示:

Revisiting this question, I have now 'discovered' 5 different ways of doing this, which are as follows:

System.ComponentModel.DesignMode property

System.ComponentModel.LicenseManager.UsageMode property

private string ServiceString()
{
    if (GetService(typeof(System.ComponentModel.Design.IDesignerHost)) != null) 
        return "Present";
    else
        return "Not present";
}

public bool IsDesignerHosted
{
    get
    {
        Control ctrl = this;

        while(ctrl != null)
        {
            if((ctrl.Site != null) && ctrl.Site.DesignMode)
                return true;
            ctrl = ctrl.Parent;
        }
        return false;
    }
}
public static bool IsInDesignMode()
{
    return System.Reflection.Assembly.GetExecutingAssembly()
         .Location.Contains("VisualStudio"))
}

要尝试摆脱提出的三种解决方案,我创建了一个带有三个项目的小测试解决方案:

To try and get a hang on the three solutions proposed, I created a little test solution - with three projects:


  • TestApp(winforms应用程序),

  • SubControl (dll)

  • SubSubControl(dll)

然后我将SubSubControl嵌入到SubControl中,

I then embedded the SubSubControl in the SubControl, then one of each in the TestApp.Form.

此屏幕截图显示了运行时的结果。

This screenshot shows the result when running.

此屏幕截图使用在Visual Studio中打开的表单显示结果:

This screenshot shows the result with the form open in Visual Studio:

结论:看来,没有反射是唯一可靠的 构造函数是LicenseUsage,唯一在外部 可靠的构造函数是 IsDesignedHosted(通过下面的BlueRaja

Conclusion: It would appear that without reflection the only one that is reliable within the constructor is LicenseUsage, and the only one which is reliable outside the constructor is 'IsDesignedHosted' (by BlueRaja below)

PS:请参阅下面ToolmakerSteve的评论(我尚未测试):请注意,IsDesignerHosted 答案已更新为包含LicenseUsage ...,因此现在测试可以简单地是if(IsDesignerHosted)。另一种方法是测试LicenseMan在构造函数中修改并缓存结果

PS: See ToolmakerSteve's comment below (which I haven't tested): "Note that IsDesignerHosted answer has been updated to include LicenseUsage..., so now the test can simply be if (IsDesignerHosted). An alternative approach is test LicenseManager in constructor and cache the result."

这篇关于带嵌套控件的DesignMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆