简体   繁体   English

Xamarin.Forms本地化

[英]Xamarin.Forms Localization

Based on the solution on github , I want to add multilanguage to my solution. 基于github上的解决方案,我想将多语言添加到我的解决方案中。 However, I have my codebehind, and not XAML, so I don't know how to do TwoWay Binding in codebehind. 但是,我的代码隐藏在后面,而不是XAML,所以我不知道如何在代码隐藏中进行TwoWay绑定。

Here is the xaml class which I want to convert to codebehind: 这是我要转换为代码隐藏的xaml类:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="myProj.Forms.Pages.LanguageSettingsPage"
         Title="{Binding Resources[Settings]}">
<StackLayout>
    <Label Text="{Binding Resources[PickLng]}" />
    <Picker ItemsSource="{Binding Languages}" SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}" />
</StackLayout>
</ContentPage>

and now my codebehind looks like: 现在我的后台代码如下:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LanguageSettingsPage : ContentPage
{
    public LanguageSettingsPage()
    {
        BindingContext = new SettingsViewModel();
        //InitializeComponent();

        StackLayout mainStack = new StackLayout();
        mainStack.BackgroundColor = Constants.iBackgroundGray;
        Label chooseLangLabel = new Label { TextColor = Constants.iGray };
        chooseLangLabel.Text = LocalizationDemoResources.PickLng;
        Picker langPicker = new Picker();
        langPicker.SelectedItem = App.CurrentLanguage;
        mainStack.Children.Add(chooseLangLabel);
        mainStack.Children.Add(langPicker);
        Content = mainStack;
    }
}

And my SettingsViewModel: 和我的SettingsViewModel:

 public class SettingsViewModel : ViewModelBase
{
    public List<string> Languages { get; set; } = new List<string>()
    {
        "EN",
        "NL",
        "FR"
    };

    private string _SelectedLanguage;
    public string SelectedLanguage
    {
        get { return _SelectedLanguage; }
        set
        {
            _SelectedLanguage = value;
            SetLanguage();
        }
    }

    public SettingsViewModel()
    {
        _SelectedLanguage = App.CurrentLanguage;
    }

    private void SetLanguage()
    {
        App.CurrentLanguage = SelectedLanguage;
        MessagingCenter.Send<object, CultureChangedMessage>(this,
                string.Empty, new CultureChangedMessage(SelectedLanguage));
    }
}

My folder with resource files looks like 我的带有资源文件的文件夹看起来像

这个

How to bind it, to get picker filled with values from my suffixes resource files? 如何绑定它,以使选择器充满后缀资源文件中的值? And how to be observable or somewhat, to be able to change languages on click of an item in picker? 以及如何在单击选择器中的某个项目时可以观察或在某种程度上能够更改语言?

Thanks. 谢谢。

SetBinding() has an overload that allows you to specify TwoWay binding SetBinding()有一个重载,可让您指定TwoWay绑定

var l = new Label();
l.SetBinding(Label.TextProperty, ".", BindingMode.TwoWay);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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