You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI项目中CommunityToolkit Mvvm命名空间找不到问题求助

解决MAUI中使用CommunityToolkit.Mvvm时的CS0246错误

问题背景

从零创建基于标准赛车模板的MAUI项目,项目本身可正常运行。安装CommunityToolkit.Mvvm及相关MAUI包后,添加ViewModel类时触发CS0246错误:找不到类型或命名空间名称“ObservableObject”,涉及net8.0-ios、net8.0-maccatalyst、net8.0-windows10.0.19041.0三个目标框架。

出错的ViewModel代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm;

namespace BelWriting.ViewModels
{
    public partial class MainPageVM : ObservableObject
    {
        [ObservableProperty]
        private int _id;
        [ObservableProperty]
        private string _name;
        [ObservableProperty]
        private string _description;

        [ICommand]
        public async void Save()
        {
            await Application.Current.MainPage.DisplayAlert("MAUI MVVM Sample", "Item Saved Successfully", "OK");
        }
    }
}

项目csproj配置片段

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
        <WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>
</Project>

问题原因及解决方法

1. 修正命名空间引用

ObservableObject、ObservableProperty属于CommunityToolkit.Mvvm.ComponentModel命名空间,[ICommand]特性属于CommunityToolkit.Mvvm.Input命名空间,替换原using指令:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

2. 验证NuGet包安装范围

  • 右键项目 → 管理NuGet程序包,确认CommunityToolkit.Mvvm已安装到所有目标框架(android、ios、maccatalyst、windows)。
  • 若安装不完整,卸载后重新安装,勾选所有目标框架选项。

3. 清理缓存并重建项目

  • 执行生成 → 清理解决方案
  • 删除项目目录下的bin和obj文件夹
  • 执行生成 → 重建解决方案

4. 目标框架兼容性排查

CommunityToolkit.Mvvm v8+完全支持.NET 8 MAUI,若仅需在Windows调试,可暂时移除iOS和MacCatalyst框架测试:
修改csproj中的TargetFrameworks节点:

<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net8.0-android;net8.0-windows10.0.19041.0</TargetFrameworks>

同时检查Windows SDK是否已正确安装(通过Visual Studio安装器确认“Universal Windows Platform开发”组件)。

内容的提问来源于stack exchange,提问作者Belight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 13:36:05