The type 'UserControl' does not support direct content

孤街醉人 提交于 2019-12-03 00:53:53
alicanerdogan

For anyone who having this problem on Visual Studio 2015, try to add (if it's not already added) System.Xaml reference to your project. Visual Studio simply fails to show reference error.

paulgai

Add System.Xaml and UIAutomationProvider references to your project, after that clear solution and then build again

SLdragon

Add System.Xaml and UIAutomationProvider references, and then restart Visual Studio solve the problems.

Just remove System.Xaml, then add it again.

In VS2017 (15.3.5) this problem occurs if the base UserControl/Window of the UserControl your are editing is in the same library/exe. After starting VS it is fine for a few minutes, then something in the background hiccoughs and the entire XAML file is blue-squiggled. Compile and it goes away, start typing and it's instantly back. Intellisense still works, but it makes the XAML editor almost unusable.

The only way to fix it is to move base classes into another library.

Try with exposing new Content property like the example and use ContentPropertyAttribute to the class. For me that helped. I had the problem in VS 2017.

[ContentProperty( "Content" )]
public class MyUserControl: UserControl
{
    public new Object Content
    {
        get => base.Content;
        set => base.Content = value;
    }
    ...
}

So it seems the coding faries have been in overnight as this now seems to work perfectly without me having changed anything, very odd, but at least I can carry on now!

Beside adding already pointed out references, I had to close and reopen solution. If even this does not solve it, restart Visual Studio.

While missing references have been mentioned as a solution, I discovered that it can also be a case of needing to resolve class ambiguities in your references.

For me, the issue was caused by an external library that had defined its own ContentPropertyAttribute in the System.Windows.Markup namespace which was causing content attributes to fail completely. Removing the reference will fix the issue, but if that is not an option, then you will have to set up a namespace alias in the reference's properties instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!