【问题标题】:How to reuse Xamarin.forms resx files in UWP native C# application如何在 UWP 本机 C# 应用程序中重用 Xamarin.forms resx 文件
【发布时间】:2021-06-23 21:58:35
【问题描述】:

我有适用于 android 和 ios 的 xamarin.forms 应用程序,我也想以 UWP 为目标,但我不想使用 Xamarin.Forms 生成的 UWP,而是我将重用 XF 共享库中的 ViewModel 和资源并原生设计 UWP。

共享项目中有 resx 文件作为翻译文件,我正在 xaml 中使用 TranslatorExtension,如下所示,如 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;
    }
}

并在 ContentPage 中的 Xaml 代码中简单地引用 <Label Text="{i18n:Translate HelloWorld}"

这工作得很好,但我不知道如何在 UWP xaml 页面中使用 translationextension。 它是否仅用于 Xamarin.Forms 应用程序。 UWP有类似的东西吗? 那么基本上如何在不复制 UWP 的情况下重用这些 resx 文件呢?

PS,如果我必须使用 Xamarin.forms nuget 和命名空间才能使用 IMarkupExtension,那对我来说仍然没问题。

【问题讨论】:

    标签: xamarin xamarin.forms uwp


    【解决方案1】:

    这工作得很好,但我不知道如何在 UWP xaml 页面中使用 translationextension。它是否仅用于 Xamarin.Forms 应用程序。 UWP有类似的东西吗?那么基本上如何在不为 UWP 复制的情况下重用这些 resx 文件呢?

    恐怕你不能在 UWP Xaml 中使用上面的TranslateExtension,因为上面的 IMarkupExtension 在特定的表单中,你不能在 UWP Xaml 中使用它。

    要进行 UWP 本地化,请参考此 document 并使用 x:Uid 指令将标记中的控件或其他元素与字符串资源标识符相关联。

    XAML

    <TextBlock x:Uid="Greeting"/>
    

    【讨论】:

    • 所以,这意味着如果不制作 Greeting.Text,我就无法使用 resx 文件。因为在 XF 中,我们不使用“.Text”样式,而只使用“Greeting”来引用文本。
    • 是的,XF xaml 还不支持 x:Uid。您也不能在表单项目中使用上述内容。
    猜你喜欢
    • 2020-12-18
    • 2019-07-04
    • 2018-12-03
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2016-10-29
    • 2019-10-02
    • 1970-01-01
    相关资源
    最近更新 更多