可以的MyUserControl不会是因为它是使用XAML定义的XAML文件的根 [英] MyUserControl cannot be the root of a XAML file because it was defined using XAML

查看:1185
本文介绍了可以的MyUserControl不会是因为它是使用XAML定义的XAML文件的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  从一个WPF用户控件继承

我试图创建一个WPF用户控件,它是从其它用户的控制,这也是我所定义的。

I'm trying to create a WPF user control, which is derived from an other user control, which I also have defined.

<myNamespace:NavigationControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:myNamespace="clr-namespace:myNamespace" mc:Ignorable="d"
x:Class="myNamespace.WelcomeScreen"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">

和这里的WelcomeScreen.cs样子:

and here's what WelcomeScreen.cs looks like:

public partial class WelcomeScreen : NavigationControl
{
    public WelcomeScreen()
    {
        this.InitializeComponent();
    }
}

当我编译,我得到以下错误:

When I compile this, I get following error:

myNamespace.NavigationControl'不能是因为它使用XAML定义的XAML文件的根。

'myNamespace.NavigationControl' cannot be the root of a XAML file because it was defined using XAML.

我在做什么错了?

推荐答案

您是不是要做到这一点,错误告诉你。

You are not meant to do that and the error tells you that.

此外,使用XAML将内容,如果你在你的派生控件重新设置它,你会失去你之前定义的一切。如果你想继承和重用使用自定义的控制(并创建一个相应的控件模板)。

Besides, using XAML you set the Content, if you set it again in your derived control you would lose everything you defined before. If you want inheritance and reusability use a custom control (and create a respective ControlTemplate).

这篇关于可以的MyUserControl不会是因为它是使用XAML定义的XAML文件的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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