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

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

问题描述

可能的重复:
从 WPF 中的 UserControl 继承

我正在尝试创建一个 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();
    }
}

编译时出现以下错误:

'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 设置 Content,如果在派生控件中再次设置它,您将丢失之前定义的所有内容.如果您想要继承和可重用性,请使用自定义控件(并创建相应的 ControlTemplate).

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天全站免登陆