的designMode与控制 [英] DesignMode with Controls

查看:155
本文介绍了的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是真的。

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是为了。 Acually获得微软内部解决它的框架将更好!

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应用程序),
  • 子控件(DLL)
  • SubSubControl(DLL)

然后我嵌入SubSubControl的子控件,那么每一个在TestApp.Form之一。

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(由<一个href="http://stackoverflow.com/questions/34664/designmode-with-controls/2693338#2693338">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)

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

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