0

I placed a control into a grid. let's say the control is derived from public class 'ButBase' which is derived in its turn from System.Windows.Controls.Button. The code normally compiles and app works just fine. But there's something really annoying.

When you try to switch to xaml-design tab it will say 'The document root element is not supported by the visual designer', which is normal and I'm totally okay with that, but the thing is, that all the xaml code is underlined and VS2010 says: 'Cannot create an instance of ButBase' although still normally compiles and able to run.

I've tried the same code in VS2008, it said that needs to see a public parameterless constructor in the ButBase, and even after I put one it showed the same error.

What do I miss here?

4

1 回答 1

1

确定ButBase不是abstract。设计师不喜欢那些。

还要确保不要在构造函数InitializedLoaded处理程序中做任何有趣的事情。有趣的包括任何重要的任务,例如连接到数据库或任何其他可能干扰设计者的事情。任何此类代码都应使用设计器检查进行包装。有很多方法可以检查设计师的存在,但我发现以下方法可以正常工作:

internal static class DesignModeChecker
{
    private static bool? _isInDesignModePriv;

    public static bool IsInDesignMode
    {
        get
        {
            if (!_isInDesignModePriv.HasValue)
            {
                _isInDesignModePriv = Process.GetCurrentProcess().ProcessName.ToLower().Trim() == "devenv" && !Debugger.IsAttached;
            }

            return _isInDesignModePriv.Value;
        }
    }
}
于 2010-04-23T17:57:43.800 回答