简体   繁体   中英

How to reuse Xamarin.forms resx files in UWP native C# application

I have xamarin.forms application for android and ios, I want to target UWP as well but i dont want to use Xamarin.Forms generated UWP rather I will reuse ViewModels and resources from XF shared library and design UWP natively.

There are resx files as translation files within the Shared project and I am using in the xaml with help of TranslatorExtension as below like mentioned in the article

[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
    const string ResourceId = "LocalizationSample.Resources.AppResources";

    static readonly Lazy resmgr = 
        new Lazy(() => 
            new ResourceManager(ResourceId, typeof(TranslateExtension)
                    .GetTypeInfo().Assembly));

    public string Text { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Text == null)
            return "";

        var ci = CrossMultilingual.Current.CurrentCultureInfo;
        var translation = resmgr.Value.GetString(Text, ci);

        if (translation == null)
        {
            translation = Text; // returns the key, which GETS DISPLAYED TO THE USER
        }
        return translation;
    }
}

And referencing in Xaml code in the ContentPage simply <Label Text="{i18n:Translate HelloWorld}"

This works perfectly fine but I couldnt figure out how to use translationextension in a UWP xaml Page. Is it only meant to be used for Xamarin.Forms application. Is there something similar for UWP. So basically how can reuse those resx files without duplicating for UWP?

PS, if i have to use Xamarin.forms nuget and namespace in order to use IMarkupExtension, it is still fine for me.

This works perfectly fine but I couldnt figure out how to use translationextension in a UWP xaml Page. Is it only meant to be used for Xamarin.Forms application. Is there something similar for UWP. So basically how can reuse those resx files without duplicating for UWP?

I'm afraid you can't use above TranslateExtension in UWP Xaml, Because above IMarkupExtension in forms specific, you can't use it in UWP Xaml.

For making UWP native localization, please refer this document and use an x:Uid directive to associate a control or other element in your markup with a string resource identifier.

XAML

<TextBlock x:Uid="Greeting"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM