Xamarin Forms XAML本地化报错:资源文件扩展名异常咨询
Hey, I’ve run into this exact problem before—super frustrating when the resource files get mislabeled as .resources instead of using the standard .resx/.resw that ResourceManager expects! Let’s walk through how to fix this step by step:
1. Verify Resource File Properties
First, check the build action and custom tool settings for your resource files—this is usually the root cause:
- In Solution Explorer, right-click your
.resx/.reswfile and select Properties. - For
.resxfiles (shared across Xamarin.Forms projects):- Set Build Action to
Embedded Resource - Set Custom Tool to
ResXFileCodeGenerator(this generates the strongly-typed resource class thatResourceManageruses)
- Set Build Action to
- For
.reswfiles (UWP-specific):- Set Build Action to
PRIResource(no custom tool needed here, UWP handles it automatically)
- Set Build Action to
2. Explicitly Target Resource Files in Your TranslateExtension
Make sure your TranslateExtension code is pointing to the correct resource file name (without any .resources suffix). Here’s an example of how to initialize the ResourceManager properly:
[ContentProperty(nameof(Text))] public class TranslateExtension : IMarkupExtension { public string Text { get; set; } public object ProvideValue(IServiceProvider serviceProvider) { if (Text == null) return string.Empty; // Use your project's namespace + resource file name (without .resx) var resourceManager = new ResourceManager("YourAppNamespace.Resources.AppResources", typeof(App).Assembly); return resourceManager.GetString(Text, CultureInfo.CurrentUICulture); } }
The key here is using the full qualified name of your resource file (e.g., YourAppNamespace.Resources.AppResources for a file named AppResources.resx in a Resources folder) instead of relying on auto-detection that might pick up the .resources artifact.
3. Clean and Rebuild Your Solution
Old build artifacts can stick around and cause this issue. Do a full clean:
- Go to Build > Clean Solution
- Manually delete the
binandobjfolders from all your project directories - Then run Build > Rebuild Solution
This ensures any leftover.resourcesfiles are removed and the new build uses your proper.resx/.reswfiles.
4. Double-Check Resource File Naming & Location
- Make sure your resource files are named correctly (e.g.,
AppResources.resxfor default culture,AppResources.es.resxfor Spanish) - Keep them in a dedicated folder like
Resourcesto avoid namespace confusion - Never manually rename the file extension to
.resources—let Visual Studio handle the build artifacts automatically
That should get your TranslateExtension working with standard .resx/.resw files again!
内容的提问来源于stack exchange,提问作者Bjorn Fontaine

