在 MAUI 中本地化选取器值的正确方法是什么?到目前为止我只有一个想法:
使用
LocalizationResourceManager.Maui
。但对于选择器值,当应用程序本地化已更改时,直到应用程序重新启动为止,它不起作用。
<Picker
x:Name="NativeLanguagePicker"
ItemsSource="{Binding NativeLanguageSetting.Options}"
SelectedItem ="{Binding NativeLanguageSetting.Picked, Mode=TwoWay}"
ItemDisplayBinding="{localization:TranslateBinding Localization, TranslateValue=True}"
Style="{DynamicResource SettingPicker}">
</Picker>
在毛伊岛有什么办法可以做到这一点吗?因为整个应用程序都遵循文化变化并进行相应的翻译(除了选择器之外)。看起来应该有一种方法可以优雅地做到这一点。
您可以参考以下解决方案。 要求是您已经为您想要的不同语言制作了资源。
让我们从实施开始。我们有一个选择器和一个单选按钮组。选择器有一个能够发挥所有魔力的行为。
<Picker x:Name="MyPicker" Title="{x:Static localization:AppResources.PickerTitle}">
<Picker.Behaviors>
<local:LocalizedPickerBehavior />
</Picker.Behaviors>
</Picker>
<local:RadioButtonGroup />
然后我们创建
Behaviour
。在这里,我们初始化 Messenger,它会告诉 Behaviour
我们已经更改了语言。
public class LocalizedPickerBehavior : Behavior<Picker>
{
protected override void OnAttachedTo(Picker picker)
{
base.OnAttachedTo(picker);
// Set the ItemsSource of the Picker
SetItemsSource(picker);
// Listen for the LanguageChanged notification
WeakReferenceMessenger.Default.Register<LocalizedPickerBehaviorMessage>(this, (recipient, message) => {
var culture = new CultureInfo(message.Value);
CultureInfo.CurrentUICulture = culture;
SetItemsSource(picker);
});
}
private void SetItemsSource(Picker picker)
{
var option1 = AppResources.Option1;
var option2 = AppResources.Option2;
var option3 = AppResources.Option3;
picker.Title = AppResources.PickerTitle;
var currentIndex = picker.SelectedIndex;
picker.ItemsSource = new List<string> { option1, option2, option3 };
picker.SelectedIndex = currentIndex;
}
protected override void OnDetachingFrom(Picker picker)
{
base.OnDetachingFrom(picker);
picker.ItemsSource = null;
WeakReferenceMessenger.Default.Unregister<LocalizedPickerBehavior>(this);
}
}
对于信使,我们需要使用我们想要更改为的语言发送一条消息。
public class LocalizedPickerBehaviorMessage : ValueChangedMessage<string>
{
public LocalizedPickerBehaviorMessage(string lang) : base(lang)
{
}
}
我们添加一个简单的单选按钮,将消息发送到我们的
Behaviour
。
public class RadioButtonGroup : StackLayout
{
public RadioButton RadioButton1 { get; private set; }
public RadioButton RadioButton2 { get; private set; }
public RadioButtonGroup()
{
Orientation = StackOrientation.Horizontal;
RadioButton1 = new RadioButton { Content = "English" };
RadioButton2 = new RadioButton { Content = "Swedish" };
RadioButton1.CheckedChanged += OnRadioButtonCheckedChanged;
RadioButton2.CheckedChanged += OnRadioButtonCheckedChanged;
Children.Add(RadioButton1);
Children.Add(RadioButton2);
RadioButton1.IsChecked = true;
}
private void OnRadioButtonCheckedChanged(object sender, CheckedChangedEventArgs e)
{
var radioButton = (RadioButton)sender;
if (radioButton == RadioButton1 && RadioButton1.IsChecked)
{
RadioButton2.IsChecked = false;
WeakReferenceMessenger.Default.Send(new LocalizedPickerBehaviorMessage("en"));
}
else if (radioButton == RadioButton2 && RadioButton2.IsChecked)
{
RadioButton1.IsChecked = false;
WeakReferenceMessenger.Default.Send(new LocalizedPickerBehaviorMessage("sv"));
}
}
}
结果是: