简体   繁体   English

使用XAML中的枚举字典和字符串中的值进行本地化

[英]Localize with values from Dictionary of enums and strings in XAML

I´m trying to databind the current culture with the right enum set in a Dictionary in Xamarin Forms XAML. 我正在尝试将当前的文化与Xamarin Forms XAML词典中设置的正确枚举进行数据绑定。

I get a list of Events from the server that contain the Dictionary properties containing the languages that I´m going to use to show the right language depending on what Culture is set. 我从服务器中获得了一个事件列表,这些事件包含包含Dictionary属性的字典,这些属性将根据设置的Culture用来显示正确的语言。

public class Event 
{
  // Having problem binding with the right CultureInfo í the Dictionary
  public Dictionary<Language, string> Title { get; set; }
}

// Types of languages that I could use to pick the right language string
public enum Language
{
    English = 0,
    German
}

// Just a basic view-model
public class EventsViewModel 
{
   public EventsViewModel()
   {
   }

    //James Montemagno´s MVVM Helper https://github.com/jamesmontemagno/mvvm-helpers
    public ObservableRangeCollection<Event> Events { get; } = new ObservableRangeCollection<Event>();
}

// Just a basic Content page public partial class EventsPage : ContentPage { public EventsPage () { InitializeComponent(); //仅是基本的Content页面公共部分类EventsPage:ContentPage {public EventsPage(){InitializeComponent(); BindingContext = new EventsViewModel(Navigation); BindingContext =新的EventsViewModel(Navigation); } } }}

Now I just need the XAML to bind the right value in the Dictionary based on the enum/culture ... 现在我只需要XAML即可根据枚举/区域性在字典中绑定正确的值 ...

I have tried doing this "semi-manual" by using a Converter and checking the CultureInfo and picking the right value but for some reason I just can´t get the Converters to fire up. 我尝试通过使用Converter并检查CultureInfo并选择正确的值执行“半手动”操作, 但是由于某些原因,我无法启动Converter。

I also tried to mix up IValueConverter and IMarkupExtension but no good. 我也尝试将IValueConverter和IMarkupExtension混合使用,但效果不佳。

Ok I found out what was missing from the Converter method I had been trying to use. 好的,我发现了我一直试图使用的Converter方法缺少的东西。 I was missing the ConvertParameter and that was the reason the Convert method did´t fire. 我缺少ConvertParameter ,这就是Convert方法没有触发的原因。

So what I did. 所以我做了。

1. 1。

I put this line into the App.xaml to be able to access it from anywhere 我将此行放入App.xaml,以便可以从任何地方访问它

 <converters:LanguageDictionaryEnumConverter x:Key="LanguageConverter" />

2. 2。

Then I put this one into my page. 然后,我将其放入页面中。 I do not use the parameter at all. 我根本不使用该参数。 (B ut I would if I could pass in my settings culture object ) 但是如果可以通过我的设置区域性对象,我会这样做

<Label Text="{Binding Title,Converter={StaticResource LanguageConverter},ConverterParameter={x:Static dataObjects:Language.English}}" />

3. 3。

And then I implemented a crude dictionary-language-enum converter/picker that chooses the right language string to send back to the page. 然后,我实现了一个粗略的字典语言枚举转换器/选择器,它选择正确的语言字符串发送回页面。

    public class LanguageDictionaryEnumConverter : IMarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dic = (Dictionary<Language, string>)value;

        var cultInMySettings= Settings.Current.GetCultureInfoOrDefault;
        switch (cultInMySettings.Name)
        {
            case "de-DE":
                return dic[Language.German];
            case "en-US":
                return dic[Language.English];
            default:
                return dic[Language.English]; 
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

But if you have some better way to databind the values straight to the XAML in some other way I want to hear about it a nd will choose that answer over this one for sure if its more elegant. 但是,如果您有更好的方法可以通过其他方法将值直接绑定到XAML,我想听听一下,并且会选择该答案而不是这个答案,以确保其更优雅。

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

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