迁移Xamarin.Forms至.NET MAUI时遇XamlC错误:无法解析ContentView/ContentPage
解决Xamarin.Forms迁移至.NET MAUI时XFC0000错误的方案
核心修复步骤
修正项目SDK配置
确保Forms项目的.csproj中添加MAUI必要属性,替换成你实际使用的.NET版本(如net6.0、net8.0):<PropertyGroup> <TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup>确保XAML命名空间完全替换
所有XAML文件的根节点必须使用MAUI命名空间,移除旧的Xamarin.Forms引用:xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"验证XAML生成操作
确认.csproj中XAML文件的生成操作已设为MauiXaml:<MauiXaml Include="YourPage.xaml" />彻底清理重建
- 删除项目目录下的
bin和obj文件夹 - 执行命令:
dotnet clean→dotnet restore→dotnet build
- 删除项目目录下的
检查MauiProgram.cs配置
确保应用入口正确注册MAUI框架:using Microsoft.Maui; using Microsoft.Maui.Hosting; namespace YourAppNamespace; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>(); return builder.Build(); } }同时确认
App.cs继承自Microsoft.Maui.Controls.Application。
额外排查点
- 自定义控件需同步更新命名空间,继承MAUI对应基类(如
Microsoft.Maui.Controls.ContentView) - 检查
.csproj中是否残留Xamarin.Forms相关的<PackageReference>条目
内容的提问来源于stack exchange,提问作者user20694704
相关产品推荐
相关产品推荐

