像许多其他人一样,我看到了这个编译错误。我是一名多年的开发人员,在多年后回到 WPF。
我有一个针对单个项目的解决方案net6.0-windows
(切换到net5.0-windows
下面进行测验)。该项目是在 VS 2022 Pro 中创建的。我没有;assembly=
在clr-namespace:
宣告中指定。在广泛阅读 SO 后,我相信这两种情况是导致此错误的最常见原因。
项目
<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
在命名空间定义中添加,因为我已经覆写了默认程序集名称。没有效果。
希望一双敏锐的眼睛能看到我犯下的愚蠢错误;不可能这幺难。预先感谢您的帮助。
uj5u.com热心网友回复:
当我完成我的问题时,我决定尝试洗掉非标准程序集名称。这就是诀窍。希望这对其他人有帮助,再次感谢您的关注。
修订项目
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<Nullable>annotations</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
0 评论