wpf 'Window' 类型不支持直接内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33805234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type 'Window' does not support direct content
提问by mrtedweb
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:
我有一个用 VS 2015 构建的 WPF 解决方案,由几个项目组成。突然间,我开始在设计模式下收到警告,说明如下:
The type 'Window' does not support direct content.
'Window' 类型不支持直接内容。
I understand how some controls do not support direct content, but System.Windows.Window
should. I get the same warning with UserControl
, and as far as I know, any other control that typically supports direct content.
我了解某些控件如何不支持直接内容,但System.Windows.Window
应该支持。我收到相同的警告UserControl
,据我所知,任何其他通常支持直接内容的控件。
Everything compiles and runs fine, but having the blue underlines through all of my XAML is bothersome. Has anyone else come across this?
一切都编译并运行良好,但在我的所有 XAML 中都带有蓝色下划线很麻烦。有没有其他人遇到过这个?
Below is a screenshot:
下面是一个屏幕截图:
回答by Mark Richman
Make sure you reference System.Xaml. Clean and rebuild the project. Works on VS 2015 Update 1.
确保您引用 System.Xaml。清理并重建项目。适用于 VS 2015 更新 1。
回答by Michael
At least in a WPF IronPython project, adding the System.Xaml
reference to the project solved the problem for me:
至少在 WPF IronPython 项目中,添加对项目的System.Xaml
引用为我解决了问题:
An important thing to note here is that adding seemingly anyreference will make the problem go away temporarily-- until Visual Studio is restarted. System.Xaml
, on the other hand, appears to keep the problem at bay. I even tried removing the reference, whereafter the problem returned upon restarting Visual Studio.
这里要注意的重要一点是,添加看似任何引用都会使问题暂时消失——直到重新启动 Visual Studio。System.Xaml
另一方面,似乎可以解决问题。我什至尝试删除引用,此后重新启动 Visual Studio 时问题又回来了。
回答by Ulysses Alves
For me this error was happening because I added a WPF Window to a class library project.
对我来说,发生此错误是因为我向类库项目添加了 WPF 窗口。
For some reason (unknown by me), Visual Studio doesn't give us the option to select the WPF Windowtemplate 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 Controltemplate for the new item, and then edited the source code to make the XAML to become a Windowobject rather than a User Control.
出于某种原因(我不知道),如果项目不是作为WPF 应用程序创建的,Visual Studio 不会为我们提供从“添加新项目...”对话框中选择WPF 窗口模板的选项。相反,它只提供添加WPF 用户控件的选项。因此,我为新项目选择了User Control模板,然后编辑源代码使 XAML 成为Window对象而不是User Control。
<!-- The new item was created as an UserControl, but what I needed was a Window object. -->
<UserControl>
...
</UserControl>
<!-- Changed it to Window and made other necessary adjustments. -->
<Window>
...
</Window>
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:
问题实际上出在代码隐藏中。由于它是作为User Control创建的,因此窗口部分类继承自UserControl,如下所示:
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.
删除继承后,Visual Studio 没有显示错误“'Window' 类型不支持直接内容”。了。
回答by dba
on behalf of @mark Richman I edited the Itemtemplate to automatically Reference "System.Xaml". Just in case some is interested:
我代表@mark Richman 编辑了 Itemtemplate 以自动引用“System.Xaml”。以防万一有人感兴趣:
can be found in: "[VS InstallDir]\Common7\IDE\ItemTemplates\VisualBasic\WPF\[InputLocale]\WPFWindow"
可以在:“[VS InstallDir]\Common7\IDE\ItemTemplates\VisualBasic\WPF\[InputLocale]\WPFWindow”中找到
BR, Daniel
BR,丹尼尔
回答by Antonio Sanchez
Add System.Xamland UIAutomationProviderreferences to your project, after that clear solution and then build again
将System.Xaml和UIAutomationProvider引用添加到您的项目中,在明确的解决方案之后,然后再次构建