【发布时间】:2022-01-19 21:15:49
【问题描述】:
像许多其他人一样,我看到了这个编译错误。我是一名资深开发人员,几年后又回到了 WPF。
我有一个针对net6.0-windows 的单一项目解决方案(切换到下面的net5.0-windows 进行测试)。该项目是在 VS 2022 Pro 中创建的。我没有在我的clr-namespace: 声明中指定;assembly=。在广泛阅读之后,我相信这两种情况是导致此错误的最常见原因。
项目
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<Nullable>annotations</Nullable>
<UseWPF>true</UseWPF>
<AssemblyName>NTS$(MSBuildProjectName)Boards</AssemblyName>
</PropertyGroup>
</Project>
用户控件
<UserControl x:Class="EndGrain.View.AssemblyPartControl"
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:vw="clr-namespace:EndGrain.View"
xmlns:vm="clr-namespace:EndGrain.ViewModel"
mc:Ignorable="d"
>
...
<Grid Background="{StaticResource BackgroundBrush}">
...
</Grid>
</UserControl>
UserControl 代码隐藏
using System.Windows.Controls;
namespace EndGrain.View
{
/// <summary>
/// Interaction logic for AssemblyPartControl.xaml
/// </summary>
public partial class AssemblyPartControl : UserControl
{
public AssemblyPartControl()
{
InitializeComponent();
}
}
}
查看
<Window x:Class="EndGrain.View.AssemblyPartSandboxView"
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:vw="clr-namespace:EndGrain.View"
mc:Ignorable="d"
Title="AssemblyPartSandboxView" Height="100" Width="200">
<Grid>
<vw:AssemblyPartControl Width="100" Height="30" />
</Grid>
</Window>
我尝试过的
从 Visual Studio Professional 2022 17.0.2 开始
- Built the project without the UserControl. Builds fine.
- Typed in the <vw:AssemblyPartControl /> element. Built the project. Error list shows the error.
- Changing the target framework back to net5.0-windows (from net6.0-windows) does not help.
使用 Visual Studio 2019 Enterprise 16.11.7 打开 net5 项目
- Rebuilt the project. Error shows.
- Commented out the UserControl. Rebuilt. Builds fine.
- Uncommented the UserControl. Rebuilt. Error shows.
使用 Blend for Visual Studio Enterprise 2019 16.11.7 打开
- Dragged the UserControl to the design surface. Blend added the element with the vw namespace.
- Built the project, and the Error List shows the error.
在命名空间定义中添加了;assembly=NTSEndGrainBoards,因为我已经覆盖了默认程序集名称。没有效果。
希望一双锐利的眼睛能看到我犯的愚蠢错误;不可能这么难。提前感谢您的帮助。
【问题讨论】: