为什么我的 UserControl 没有显示在设计器中? [英] Why does my UserControl not display in the designer?

查看:45
本文介绍了为什么我的 UserControl 没有显示在设计器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕.基本上它定义了两个依赖属性 MainContentUserInteractions,然后它们显示在可视化模板(ResourceDictionary 中的 xaml)中,如下所示:

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:

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

屏幕的 Xaml 如下所示:

Xaml for a screen then looks like this:

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

这在我运行应用程序时工作正常.但是,在设计器中,什么都看不到.不是视图中明确定义的内容,也不是模板中的内容.我需要添加什么才能启用设计支持?我尝试按照某些地方的建议将模板移至 Themes/Generic.xaml,但这没有任何区别.这个问题似乎相关,但没有用回答.

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. This SO question seems related, but gets no useful answer.

我的 ScreenControl 看起来像这样:

My ScreenControl looks like this:

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.

在使用控件时,我创建了一个 UserControl,添加了问题开头显示的 Xaml,并删除了代码隐藏文件.

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.

推荐答案

为了应用模板,您必须从 Control 而不是 UserControl 继承自定义控件.

You must inherit your custom control from Control and not UserControl for having the template applied.

根据您提供的信息很难判断,但您必须有一个应用模板的静态构造函数.

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)));
    }
}

在进一步阅读时可能不是您的问题,不确定您在某处有一些 InDesignMode 吗?调用仅在应用程序运行时才有效的代码?IE WebService 调用?这里只是猜测,但很多事情可能会导致设计者崩溃.

May not be your issue when reading further, not sure you have some InDesignMode somewhere? Calls in your code that only works when the application is running? IE WebService calls? Just guessing here, but alot of things may cause the designer to break.

这篇关于为什么我的 UserControl 没有显示在设计器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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