The type 'Window' does not support direct content

前端 未结 5 724
既然无缘
既然无缘 2021-02-05 00:09

I have a WPF solution built with VS 2015 composed of several projects. Suddenly I started receiving a warning in design mode stating the following:

The t

5条回答
  •  孤独总比滥情好
    2021-02-05 00:54

    For me this error was happening because I added a WPF Window to a class library project.

    For some reason (unknown by me), Visual Studio doesn't give us the option to select the WPF Window template from the "Add New Item..." dialog box if the project was not created as a WPF Application. Instead, it only offers the option to add a WPF User Control. Because of that, I selected the User Control template for the new item, and then edited the source code to make the XAML to become a Window object rather than a User Control.

    
    
       ...
    
    
    
    
       ...
    
    

    The problem was actually in the code-behind. Since it was created as an User Control, the window partial class was inheriting from UserControl, like the following:

    public partial class MyWindow : UserControl
    {
        public MyWindow ()
        {
            InitializeComponent();
        }
    }
    

    To fix it I just had to remove the inheritance, making my window class inherith from nothing, like this:

    public partial class MyWindow
    {
        public MyWindow ()
        {
            InitializeComponent();
        }
    }
    

    After removing the inheritance, Visual Studio didn't show the error "The type 'Window' does not support direct content." anymore.

提交回复
热议问题