基于另一个类的 UiElement

发布于 2024-08-29 17:49:30 字数 341 浏览 6 评论 0原文

我将一个控件放入网格中。假设该控件派生自公共类“ButBase”,而“ButBase”又派生自 System.Windows.Controls.Button。代码通常可以编译并且应用程序运行得很好。但有件事真的很烦人。

当您尝试切换到 xaml-design 选项卡时,它会显示“可视化设计器不支持文档根元素”,这是正常的,我完全同意,但问题是,所有 xaml 代码都是下划线和 VS2010 说:“无法创建 ButBase 的实例”,尽管仍然可以正常编译并能够运行。

我在VS2008中尝试过相同的代码,它说需要在ButBase中看到一个公共无参数构造函数,即使在我放置一个之后它也显示相同的错误。

我在这里想念什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-09-05 17:49:30

确保 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;
        }
    }
}

Make sure ButBase is not abstract. Designer does not like those.

Also make sure not to do any funny business in your constructor, Initialized and Loaded handlers. Funny includes any non-trivial tasks, such as connecting to a database or anything else that might interfere with the designer. Any such code should be wrapped with a designer-check. There are a number of ways out there to check for a presence of designer but I have found the following to work ok:

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文