C# 为什么我的UserControl不显示在设计器中?
我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕。基本上,它定义了两个依赖属性C# 为什么我的UserControl不显示在设计器中?,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕。基本上,它定义了两个依赖属性MainContent和UserInteractions,它们随后显示在可视化模板中(在ResourceDictionary中的xaml),如下所示: +-------------+ | L | | | o | Main | | g | Content | | o | | +---+---------+ | Interaction | +-------------+
MainContent
和UserInteractions
,它们随后显示在可视化模板中(在ResourceDictionary
中的xaml),如下所示:
+-------------+
| L | |
| o | Main |
| g | Content |
| o | |
+---+---------+
| Interaction |
+-------------+
屏幕的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>
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)));
}
在设计器中查看使用控件的屏幕时,仅显示以下内容:
没有,只有一个空盒子
使用该控件时,我正在创建一个
UserControl
,添加问题开头显示的Xaml,并删除隐藏的代码文件。要应用模板,必须从control而不是UserControl继承自定义控件
您提供的信息很难判断,但是您必须有一个应用模板的静态构造函数
public class ScreenControl : Control
{
static ScreenControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenControl), new FrameworkPropertyMetadata(typeof(ScreenControl)));
}
}
当进一步阅读时,可能不是您的问题,不确定您是否在某个地方有一些InDesignMode?
调用仅在应用程序运行时有效的代码?IE网络服务呼叫?这里只是猜测,但很多事情可能会导致设计器崩溃。模板已应用,因此它在运行时工作,但在设计器模式下,它只是一个空白页(甚至不是设计器例外)。我将试图澄清这个问题。实际上,目前还没有外部电话或其他任何安排。只是视觉效果。我尝试从
Control
继承,而不是从UserControl
,但在设计时或运行时都看不到任何差异。重写OnApplyTemplate方法并在那里设置断点,看看它是否在设计器中被调用。(对于像我一样不知道如何调试设计器的未来读者,请检查)这个被接受的答案是实际的解决方案吗?是否与WPF相关?我在WinRT XAML中遇到了完全相同的问题,提出的解决方案根本无法编译。