Use strings in .resw File directly in XAML

前端 未结 2 1051
逝去的感伤
逝去的感伤 2021-01-22 08:07

I know the usual way to reference localized strings from a .resw file would be like this:

XAML:

Re

2条回答
  •  误落风尘
    2021-01-22 08:31

    You can use a CustomXamlResourceLoader:

    public class XamlResourceLoader : CustomXamlResourceLoader
    {
        private readonly ResourceLoader _loader;
    
        public XamlResourceLoader()
        {
            _loader = ResourceLoader.GetForViewIndependentUse();
        }
    
        protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType)
        {
            return _loader.GetString(resourceId) ?? resourceId;
        }
    }
    

    Then in your App.xaml.cs constructor:
    CustomXamlResourceLoader.Current = new XamlResourceLoader();

    And finally in your xaml:

提交回复
热议问题