【问题标题】:System.Windows.Markup.XamlParseException in InitializeComponent() (UserControl)InitializeComponent() (UserControl) 中的 System.Windows.Markup.XamlParseException
【发布时间】:2018-03-18 08:15:49
【问题描述】:

对于答案,请参见下文。 (https://stackoverflow.com/a/46615550/6826935)


我正在做 WPF 开发并收到此错误。

System.Windows.Markup.XamlParseException 错误 初始化组件();

代码摘录:(MainWindow.xaml.cs)

public MainWindow()
    {
        InitializeComponent(); // Exception here
        this.DataContext = this;

        // UI stuff here, deleted
    }

异常说明了这一点

为类型集合添加值 'System.Windows.Controls.UIElementCollection' 引发了异常。 行号“23”和行位置“10”。

所以这里是相关文件 (MainWindow.xaml) 中的第 23-24 行

xmlns:local="clr-namespace:Cozyplanes.SudokuApp"

// UI stuff here..., deleted

<local:SudokuUserControl x:Name="SudokuGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="454" Height="451"
                              Loaded="SudokuGrid_Loaded" KeyUp="SudokuGrid_KeyUp" MouseLeftButtonUp="SudokuGrid_MouseLeftButtonUp"/>

我不知道为什么会这样。 MainWindow 类正在扩展 Window。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window { ... }

作为参考, SudokuUserControl.xaml.cs

/// <summary>
/// Interaction logic for SudokuUserControl.xaml
/// </summary>
public partial class SudokuUserControl : Window { ... }

在输出中

Step into: Stepping over non-user code 'Cozyplanes.SudokuApp.App.InitializeComponent'

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 这有关系吗? Module is optimized and the debugger option 'Just My Code' is enabled.
  • SudokuUserControl 继承自 Window。您不能将 Window 添加为子控件。如果你想真正将它用作控件,它应该从UserControl 继承,修复它可能涉及的不仅仅是更改基类。
  • 那么我应该从什么修复到什么?
  • 您需要将SudokuUserControl 实现为UserControl,而不是Window。它可能就像将public partial class SudokuUserControl : Window 更改为public partial class SudokuUserControl : UserControl、将XAML 的根元素修改为UserControl 并清理UserControl 不支持的属性一样简单。或者这可能会困难得多;如果没有看到SudokuUserControl 的更多代码和 XAML,很难说。
  • @BradleyUffner 非常感谢。我完全忘记了modifying the root element of the XAML to UserControl。它被设置为Window。是的,我有点累了。无论如何,非常感谢你!

标签: c# wpf xaml exception


【解决方案1】:

好的,这就是未来的答案~~~~~!!!!!! (好吧,我闭嘴)

所以错误发生在MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent(); // Exception here
    this.DataContext = this;

    // UI stuff here, deleted
}

它说它在MainWindow.xaml的第23行有一个例外

xmlns:local="clr-namespace:Cozyplanes.SudokuApp"

// UI 的东西在这里...,已删除

现在你会注意到那里有SudokuUserControl

我的项目是这样制作的。 SudokuUserControl.xaml 位于 MainWindow.xaml 的顶部

MainWindow.xaml

|--- MainWindow.xaml.cs

SudokuUserControl.xaml

|--- SudokuUserControl.xaml.cs

SudokuUserControl 的东西在MainWindow.xaml 文件中被引用。

MainWindow.xaml.cs 文件正在扩展 WindowSudokuUserControl.xaml.cs 没有扩展任何内容。

所以布拉德利告诉我检查文件的根元素。 我检查了一下,发现根元素是Window,而不是UserControl

因此,当您收到 InitializeComponent() 错误时,请确保您引用了 XAML 文件和 XAML CS 文件中的内容。


现在就是这样。如果您需要问题,我会尽力帮助您!希望能帮助到你。谢谢,布拉德利。你是救生员。 :P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2017-09-20
    • 2011-04-27
    相关资源
    最近更新 更多