【问题标题】:Different base class in the XAML fileXAML 文件中的不同基类
【发布时间】:2012-09-08 16:49:41
【问题描述】:

我有一个名为 BIFUserControl 的类,它继承自 UserControl 类。现在我正在设计一个名为BIFText 的新用户控件,它继承自BIFUserControl 类。因此,我将名为 BIFText.xaml 的 XAML 文件更改如下:

<base:BIFUserControl 
              xmlns:base="clr-namespace:BaseInputFramework.BaseWidgets;assembly=BaseInputFramework"
              x:Class="BIFWidgetLibrary.Text.BIFText"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

              xmlns:mp="clr-namespace:Microsoft.Multipoint.Sdk.Controls;assembly=Microsoft.Multipoint.Sdk.Controls"
              xmlns:utils="clr-namespace:BaseInputFramework.BaseWidgets.Utils;assembly=BaseInputFramework"

              mc:Ignorable="d" 
              d:DesignHeight="150" d:DesignWidth="150">
     <Grid>


     </Grid> </base:BIFUserControl>

然后我更改了我的BIFText.xaml.cs 文件如下:

 namespace BIFWidgetLibrary.Text {
     public partial class BIFText : BIFUserControl
     {
         public BIFText()
         {
             InitializeComponent();
         }
     } }

但是现在当我尝试构建项目时,我收到以下错误消息: 'BaseInputFramework.BaseWidgets.BIFUserControl' cannot be the root of a XAML file because it was defined using XAML. Line 2 Position 14.

有人可以帮我解决这个错误吗?提前致谢。

【问题讨论】:

  • 您的BIFUserControl 的模板是否也有一些 xaml 文件,或者您只有从 UserControl 继承它?
  • 是的,BIFUserControl 类也有自己的 XAML 文件。

标签: c# wpf xaml


【解决方案1】:

错误表明BaseInputFramework.BaseWidgets.BIFUserControl 不能是 XAML 文件的根,因为它是使用 XAML 定义的。只有没有使用 XAML 文件定义的元素只能是根元素。请参阅这些链接 - Cannot define root elementInheriting from UserControl

【讨论】:

  • 是的,你在写,我很快就制作了一个没有 XAML 文件的新 Base UserControl 类。之后,上面的代码对我有用。谢谢!
【解决方案2】:

UserControls 通过将Content 设置为您在 XAML 中定义的值来工作,这可能是您不能这样继承的原因:基类的内容将被覆盖。

如果您不介意完全替换内容,不妨使用自定义控件并定义Template

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2013-05-28
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多