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

Xamarin Forms XAML本地化报错:资源文件扩展名异常咨询

Fixing the .resources Extension Issue with TranslateExtension in Xamarin.Forms

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/.resw file and select Properties.
  • For .resx files (shared across Xamarin.Forms projects):
    • Set Build Action to Embedded Resource
    • Set Custom Tool to ResXFileCodeGenerator (this generates the strongly-typed resource class that ResourceManager uses)
  • For .resw files (UWP-specific):
    • Set Build Action to PRIResource (no custom tool needed here, UWP handles it automatically)

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 bin and obj folders from all your project directories
  • Then run Build > Rebuild Solution
    This ensures any leftover .resources files are removed and the new build uses your proper .resx/.resw files.

4. Double-Check Resource File Naming & Location

  • Make sure your resource files are named correctly (e.g., AppResources.resx for default culture, AppResources.es.resx for Spanish)
  • Keep them in a dedicated folder like Resources to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:23:58