WinUI 3无法导入Community Toolkit SettingsCard问题求助
如何在WinUI 3 XAML中正确添加SettingsCard控件?
问题描述
我是WinUI 3的新手,在使用Community Toolkit的SettingsCard控件时遇到困难,无法在XAML中导入该控件,想请教是否是.NET SDK版本的问题。以下是我的XAML代码和项目配置文件:
原XAML代码
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="name.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:name" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:control="using:CommunityToolkit.WinUI" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <controls:SettingsCard x:Name="settingsCard" Description="This is a default card, with the Header, HeaderIcon, Description and Content set." Header="This is the Header"></controls:SettingsCard> </StackPanel> </Window>
项目配置文件
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework> <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion> <RootNamespace>name</RootNamespace> <ApplicationManifest>app.manifest</ApplicationManifest> <Platforms>x86;x64;ARM64</Platforms> <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers> <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) < 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers> <PublishProfile>win-$(Platform).pubxml</PublishProfile> <UseWinUI>true</UseWinUI> <EnableMsixTooling>true</EnableMsixTooling> </PropertyGroup> <ItemGroup> <Content Include="Assets\SplashScreen.scale-200.png" /> <Content Include="Assets\LockScreenLogo.scale-200.png" /> <Content Include="Assets\Square150x150Logo.scale-200.png" /> <Content Include="Assets\Square44x44Logo.scale-200.png" /> <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" /> <Content Include="Assets\StoreLogo.png" /> <Content Include="Assets\Wide310x150Logo.scale-200.png" /> </ItemGroup> <ItemGroup> <PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.1.240916" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" /> <Manifest Include="$(ApplicationManifest)" /> </ItemGroup> <!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging Tools extension to be activated for this project even if the Windows App SDK Nuget package has not yet been restored. --> <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> <ProjectCapability Include="Msix" /> </ItemGroup> <!-- Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution Explorer "Package and Publish" context menu entry to be enabled for this project even if the Windows App SDK Nuget package has not yet been restored. --> <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu> </PropertyGroup> </Project>
解决方案
问题根源
这不是.NET SDK版本的问题,核心错误是XAML中引用的命名空间不对。
修正步骤
修改XAML命名空间
SettingsCard控件属于CommunityToolkit.WinUI.Controls.SettingsControls命名空间,你之前写的CommunityToolkit.WinUI是错误的。需要把命名空间声明改成:xmlns:controls="using:CommunityToolkit.WinUI.Controls.SettingsControls"注意保持控件前缀和命名空间前缀一致(这里用
controls:,你也可以自定义前缀名称)。验证包引用
你的项目已经正确安装了CommunityToolkit.WinUI.Controls.SettingsControls包,这部分无需调整。修正后的完整XAML代码
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="name.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:name" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:CommunityToolkit.WinUI.Controls.SettingsControls" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <controls:SettingsCard x:Name="settingsCard" Description="This is a default card, with the Header, HeaderIcon, Description and Content set." Header="This is the Header"></controls:SettingsCard> </StackPanel> </Window>
额外排查建议
- 如果修改后仍报错,试试清理项目(菜单栏→项目→清理解决方案),再重新生成项目(菜单栏→生成→重新生成解决方案)。
- 当前你的Windows App SDK版本和Community Toolkit版本是兼容的,不需要调整版本。
内容的提问来源于stack exchange,提问作者Happer64Bit
相关产品推荐
相关产品推荐

