car*_*ett 5 c# wpf xaml user-controls
我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕。基本上它定义了两个依赖属性MainContent
,UserInteractions
然后显示在可视化模板(a 中的 xaml ResourceDictionary
)中,如下所示:
+-------------+
| L | |
| o | Main |
| g | Content |
| o | |
+---+---------+
| Interaction |
+-------------+
Run Code Online (Sandbox Code Playgroud)
屏幕的 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>
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,这很好用。但是,在设计器中,什么都看不到。不是视图中明确定义的内容,也不是模板中的内容。我需要添加什么才能启用设计支持?我尝试将模板移动到Themes/Generic.xaml
某些地方的建议,但这没有区别。这个 SO 问题似乎相关,但没有得到有用的答案。
编辑:
我的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)));
}
Run Code Online (Sandbox Code Playgroud)
在设计器中查看使用该控件的屏幕时,它仅显示以下内容:
即,什么都没有,只有一个空白框。
使用控件时,我创建了一个UserControl
,添加了问题开头显示的 Xaml,并删除了代码隐藏文件。
您必须从 Control 而不是 UserControl 继承自定义控件才能应用模板。
很难用您提供的信息来判断,但您必须有一个应用模板的静态构造函数。
public class ScreenControl : Control
{
static ScreenControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ScreenControl), new FrameworkPropertyMetadata(typeof(ScreenControl)));
}
}
Run Code Online (Sandbox Code Playgroud)
进一步阅读时可能不是您的问题,不确定您是否在某处有 InDesignMode?您的代码中的调用仅在应用程序运行时才有效?IE WebService 调用?这里只是猜测,但很多事情可能会导致设计师崩溃。