声明System.ComponentModel的XAML命名空间遇报错,如何解决?
WPF中导入System.ComponentModel命名空间提示程序集未找到
你已尝试为WPF项目(.NET 9)添加System.ComponentModel NuGet包(4.3.0版本),并在XAML中以scm别名导入命名空间,但编译时提示Assembly 'System.ComponentModel.dll' was not found。
项目配置代码
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net9.0-windows</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <UseWPF>true</UseWPF> </PropertyGroup> <ItemGroup> <PackageReference Include="System.ComponentModel" Version="4.3.0" /> </ItemGroup> </Project>
XAML中的命名空间声明及完整代码
命名空间声明:
xmlns:scm="clr-namespace:System.ComponentModel;assembly=System.ComponentModel.dll"
完整用户控件XAML:
<UserControl x:Class="VectorBasedIConTrial.DynamicResourceTrialOne" 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:scm="clr-namespace:System.ComponentModel;assembly=System.ComponentModel.dll" xmlns:local="clr-namespace:VectorBasedIConTrial" xmlns:systemns="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded" > <UserControl.Resources> <CollectionViewSource x:Key="ModulesViewSource" Source="{Binding Path=Modules}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Name" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource> </UserControl.Resources> <Grid> </Grid> </UserControl>
错误信息
Assembly 'System.ComponentModel.dll' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built.
解决步骤
卸载不必要的NuGet包
在.NET 5+(包括.NET 9)中,System.ComponentModel命名空间下的核心类型(如SortDescription)已经内置在框架基础库中,无需额外安装System.ComponentModelNuGet包。直接从项目中移除该包引用即可。修正XAML命名空间声明
删除命名空间声明中的assembly=System.ComponentModel.dll部分,正确的声明为:xmlns:scm="clr-namespace:System.ComponentModel"因为该命名空间属于.NET框架核心程序集,无需指定assembly名称,框架会自动查找对应的程序集。
清理并重建项目
执行项目清理(右键项目→清理),然后重新生成项目,避免缓存的错误信息干扰。
原因说明
早期.NET Framework版本需要显式引用独立的System.ComponentModel.dll程序集,但.NET Core及后续版本(.NET 5+)将基础库进行了整合,这类通用命名空间的类型已包含在默认的框架引用中,额外安装旧版NuGet包会导致程序集版本冲突或查找路径错误,从而引发找不到程序集的问题。
内容的提问来源于stack exchange,提问作者VivekDev
相关产品推荐
相关产品推荐

