简体   繁体   English

为什么我的UserControl不会显示在设计器中?

[英]Why does my UserControl not display in the designer?

I have implemented a user control that lets me build several similar interface screens quickly. 我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕。 Basically it defines two dependency properties MainContent and UserInteractions which are then displayed in the visual template (xaml in a ResourceDictionary ) looking like this: 基本上,它定义了两个依赖项属性MainContentUserInteractions ,然后显示在可视模板( ResourceDictionary xaml)中,如下所示:

+-------------+
| L |         |
| o |  Main   |
| g | Content |
| o |         |
+---+---------+
| Interaction |
+-------------+

Xaml for a screen then looks like this: 然后屏幕的Xaml看起来像这样:

<controls:ScreenControl>
    <controls:ScreenControl.MainContent>
        <TextBlock>Some content goes here</TextBlock>
    </controls:ScreenControl.MainContent>
    <controls:ScreenControl.UserInteractions>
        <Button>Do something</Button>
    </controls:ScreenControl.UserInteractions>
</controls:InstallerScreenControl>

This works fine when I run the application. 这在我运行应用程序时工作正常。 However, in the designer, nothing is visible. 但是,在设计师中,没有任何东西可见。 Not the content defined explicitly in the view, and not the stuff from the template. 不是视图中明确定义的内容,也不是模板中的内容。 What do I need to add to enable design support? 我需要添加什么才能启用设计支持? I tried moving the template to Themes/Generic.xaml as was suggested in some places, but that made no difference. 我尝试将模板移动到Themes/Generic.xaml如某些地方所建议的那样,但这没有任何区别。 This SO question seems related, but gets no useful answer. 这个问题似乎是相关的,但没有得到有用的答案。

EDIT: My ScreenControl looks like this: 编辑:我的ScreenControl看起来像这样:

public class ScreenControl : UserControl
{
    public object MainContent
    {
        get { return GetValue(MainContentProperty); }
        set { SetValue(MainContentProperty, value); }
    }
    public static readonly DependencyProperty MainContentProperty = DependencyProperty.Register(
        name: "MainContent",
        propertyType: typeof(object), 
        ownerType: typeof(ScreenControl),
        typeMetadata: new PropertyMetadata(default(object)));


    public object UserInteractions
    {
        get { return GetValue(UserInteractionsProperty); }
        set { SetValue(UserInteractionsProperty, value); }
    }
    public static readonly DependencyProperty UserInteractionsProperty = DependencyProperty.Register(
        name: "UserInteractions",
        propertyType: typeof(object),
        ownerType: typeof(ScreenControl),
        typeMetadata: new PropertyMetadata(default(object)));
}

When a screen using the control is viewed in the designer, it only shows this: 在设计器中查看使用该控件的屏幕时,它仅显示以下内容:

这里没有什么...

Ie, nothing, only a blank box. 即,没有,只有一个空白框。

When using the control, I'm creating a UserControl , adding the Xaml shown in the beginning of the question, and removing the code-behind file. 使用控件时,我正在创建一个UserControl ,添加问题开头显示的Xaml,并删除代码隐藏文件。

You must inherit your custom control from Control and not UserControl for having the template applied. 您必须从Control继承自定义控件,而不是UserControl才能应用模板。

It's hard to tell with the information you are giving, but you must have a static constructor that applies the template. 很难用你提供的信息来判断,但你必须有一个应用模板的静态构造函数。

public class ScreenControl : Control
{
    static ScreenControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenControl), new FrameworkPropertyMetadata(typeof(ScreenControl)));
    }
}

May not be your issue when reading further, not sure you have some InDesignMode somewhere? 在进一步阅读时可能不是你的问题,不确定你在某处有一些InDesignMode? Calls in your code that only works when the application is running? 您的代码中的调用仅在应用程序运行时有效? IE WebService calls? IE WebService调用? Just guessing here, but alot of things may cause the designer to break. 只是在这里猜测,但很多事情可能会导致设计师破产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM