带有嵌套控件的设计模式

DesignMode with nested Controls(带有嵌套控件的设计模式)

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

问题描述

有没有人在开发控件时找到了设计模式问题的有用解决方案?

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

然后我将 SubSubControl 嵌入到 SubControl 中,然后在 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,唯一可靠外部em> 构造函数是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).另一种方法是 在构造函数中测试 LicenseManager 并缓存结果."

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."

这篇关于带有嵌套控件的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有嵌套控件的设计模式

基础教程推荐