MAUI中XAML无法引用resx字符串资源报错排查
MAUI XAML引用本地化资源出现XFC0000错误
在Windows系统使用Visual Studio 17.4.4开发MAUI应用时,后台代码可正常访问MyApp.Resources.Localization.AppStrings,但在XAML中引用时触发XamlC错误XFC0000:无法解析类型"MyApp.Resources.Localization:AppStrings"。
相关代码及文件信息
XAML代码
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="MyApp.Resources.Localization" xmlns:views="clr-namespace:Jetty_Mobile.Views" x:Name="thisPage" x:Class="MyApp.MainPage"> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <Label Text="{x:Static local:AppStrings.AppName}" /> </VerticalStackLayout> </ContentPage>
后台可正常运行的代码
public MainPage() { InitializeComponent(); var thisworks = MyApp.Resources.Localization.AppStrings.AppName; }
Resx文件说明
AppStrings.resx位于MyApp.Resources.Localization目录下,已自动生成对应的Designer.cs文件。
AppStrings.Designer.cs代码
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace MyApp.Resources.Localization { using System; /// <summary> /// A strongly-typed resource class, for looking up localized strings, etc. /// </summary> // This class was auto-generated by the StronglyTypedResourceBuilder // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] public class AppStrings { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal AppStrings() { } /// <summary> /// Returns the cached ResourceManager instance used by this class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] public static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Jetty_Mobile.Resources.Localization.AppStrings", typeof(AppStrings).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] public static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } /// <summary> /// Looks up a localized string similar to My App. /// </summary> public static string AppName { get { return ResourceManager.GetString("AppName", resourceCulture); } } } }
排查与修复步骤
修正命名空间不匹配问题
观察Designer.cs中的ResourceManager初始化代码,发现使用了"Jetty_Mobile.Resources.Localization.AppStrings",但应用根命名空间是MyApp,这是核心矛盾:- 右键
AppStrings.resx文件,选择属性; - 将自定义工具命名空间修改为
MyApp.Resources.Localization; - 删除现有的
AppStrings.Designer.cs,右键resx文件选择运行自定义工具重新生成设计器代码。
- 右键
完善XAML命名空间声明
修改XAML中的local命名空间声明,显式指定程序集(需与项目程序集名称一致):xmlns:local="clr-namespace:MyApp.Resources.Localization;assembly=MyApp"清理重建项目
执行Build -> Clean Solution清理解决方案,再执行Build -> Rebuild Solution重建,清除XamlC的缓存文件,避免旧代码干扰。验证Resx文件属性
确保AppStrings.resx的生成操作为嵌入式资源,自定义工具为ResXFileCodeGenerator,这是设计器代码正确生成的前提。
内容的提问来源于stack exchange,提问作者John Ureke
相关产品推荐
相关产品推荐

