如何绑定并获得resx的字词(本地化) [英] How to bind and get word of resx (localization)

查看:82
本文介绍了如何绑定并获得resx的字词(本地化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了这篇文章:我想绑定并获取我的.resx

 Title="{x:Static resx:AppResources.Binding Title}">

它们的标题值是我的ViewModel中的Road, Fly, Run

their value of title is Road, Fly, Run from my ViewModel

<data name="Road" xml:space="preserve">
        <value>Camino</value>
    </data>

推荐答案

我已经按照以下步骤在本地站点中进行了检查,并且可以正常工作.

I have checked that in local site as follow steps , and it works .

创建一个 AppResources.resx 文件:

  <data name="Fly" xml:space="preserve">
    <value>DefaultFlyRoute</value>
  </data>
  <data name="Road" xml:space="preserve">
    <value>DefaultRoad</value>
  </data>
  <data name="Run" xml:space="preserve">
    <value>DefaultRunRoute</value>
  </data>

ViewModel 类中,用于在初始化时加载.resx数据作为默认值.

In ViewModel class to load .resx data as default value when initialiation .

public class ViewModel
{
    public string Road { get; set; }
    public string Fly { get; set; }
    public string Run { get; set; }


    public ViewModel()
    {
        Road = AppResources.Road;
        Fly = AppResources.Fly;
        Run = AppResources.Run;
    }
}

Xaml 代码如下,以绑定ViewModel中的值:

The Xaml code as follow to bind value from ViewModel :

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:AppResxFile"
             mc:Ignorable="d"
             x:Class="AppResxFile.MainPage">

    <ContentPage.BindingContext>
        <local:ViewModel />
    </ContentPage.BindingContext>
    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Label Text="{Binding Road}" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Label Text="{Binding Fly}" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Label Text="{Binding Run}" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    </StackLayout>

</ContentPage>

最终效果如下:

这篇关于如何绑定并获得resx的字词(本地化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆