diff --git a/JackCraft.I18N/I18NBinding.cs b/JackCraft.I18N/I18NBinding.cs index 1adaad0..61a8dc6 100644 --- a/JackCraft.I18N/I18NBinding.cs +++ b/JackCraft.I18N/I18NBinding.cs @@ -9,7 +9,7 @@ internal class I18NBinding : MultiBinding public readonly IValueConverter KeyConverter; public readonly IValueConverter ValueConverter; - public I18NBinding(string key, params dynamic[] values) + public I18NBinding(object key, params dynamic[] values) { Mode = BindingMode.OneWay; TargetNullValue = key; @@ -18,13 +18,26 @@ internal class I18NBinding : MultiBinding KeyConverter = new I18NKeyConverter(); ValueConverter = new I18NValueConverter(); Bindings.Add(new Binding { Source = I18NConfig.Manager, Path = nameof(I18NConfig.Manager.Culture) }); - Bindings.Add(new Binding { Source = key }); - foreach (var value in values) + + switch (key) { - if (value is BindingBase binding) + // 处理 key 参数,可能是字符串或 BindingBase + case string keyStr: + Bindings.Add(new Binding { Source = keyStr }); + break; + case BindingBase binding: Bindings.Add(binding); + break; + // 其他情况,作为普通对象处理 + default: + Bindings.Add(new Binding { Source = key }); + break; + } + + foreach (var value in values) + if (value is BindingBase valueBinding) + Bindings.Add(valueBinding); else Bindings.Add(new Binding { Source = value }); - } } } \ No newline at end of file diff --git a/JackCraft.I18N/I18NExtension.cs b/JackCraft.I18N/I18NExtension.cs index 5a4a8be..862e9f9 100644 --- a/JackCraft.I18N/I18NExtension.cs +++ b/JackCraft.I18N/I18NExtension.cs @@ -1,9 +1,19 @@ +using Avalonia.Data; using Avalonia.Markup.Xaml; namespace JackCraft.I18N; -public class I18NExtension(string key, params dynamic[] values) : MarkupExtension +public class I18NExtension : MarkupExtension { + private readonly object _key; + private readonly dynamic[] _values; + + public I18NExtension(string key, params dynamic[] values) + { + _key = key; + _values = values; + } + public I18NExtension(string key) : this(key, []) { } @@ -31,8 +41,43 @@ public class I18NExtension(string key, params dynamic[] values) : MarkupExtensio { } + // 支持 BindingBase 作为 key + public I18NExtension(BindingBase key, params dynamic[] values) + { + _key = key; + _values = values; + } + + public I18NExtension(BindingBase key) : this(key, []) + { + } + + public I18NExtension(BindingBase key, dynamic value1) : this(key, [value1]) + { + } + + public I18NExtension(BindingBase key, dynamic value1, dynamic value2) : this(key, [value1, value2]) + { + } + + public I18NExtension(BindingBase key, dynamic value1, dynamic value2, dynamic value3) : this(key, + [value1, value2, value3]) + { + } + + public I18NExtension(BindingBase key, dynamic value1, dynamic value2, dynamic value3, dynamic value4) : this(key, + [value1, value2, value3, value4]) + { + } + + public I18NExtension(BindingBase key, dynamic value1, dynamic value2, dynamic value3, dynamic value4, + dynamic value5) : + this(key, [value1, value2, value3, value4, value5]) + { + } + public override object ProvideValue(IServiceProvider serviceProvider) { - return new I18NBinding(key, values); + return new I18NBinding(_key, _values); } } \ No newline at end of file diff --git a/TestMvvm/Assets/Resources.resx b/TestMvvm/Assets/Resources.resx index 53651eb..ee65c91 100644 --- a/TestMvvm/Assets/Resources.resx +++ b/TestMvvm/Assets/Resources.resx @@ -1,7 +1,8 @@ - @@ -26,4 +27,7 @@ Test1: {0} + + Test2Test2 + \ No newline at end of file diff --git a/TestMvvm/Assets/Resources.zh-hans.resx b/TestMvvm/Assets/Resources.zh-hans.resx index bbdc8f4..ee63f84 100644 --- a/TestMvvm/Assets/Resources.zh-hans.resx +++ b/TestMvvm/Assets/Resources.zh-hans.resx @@ -18,4 +18,7 @@ 测试1: {0} + + 测试2测试2 + \ No newline at end of file diff --git a/TestMvvm/ViewModels/MainWindowViewModel.cs b/TestMvvm/ViewModels/MainWindowViewModel.cs index 082a5f9..4efc7ab 100644 --- a/TestMvvm/ViewModels/MainWindowViewModel.cs +++ b/TestMvvm/ViewModels/MainWindowViewModel.cs @@ -7,7 +7,8 @@ namespace TestMvvm.ViewModels; public partial class MainWindowViewModel : ViewModelBase { - [ObservableProperty] public string? test = Path.GetRandomFileName(); + [ObservableProperty] private string? _test = Path.GetRandomFileName(); + [ObservableProperty] private string _test2 = "Test2"; [RelayCommand] public void RandomText() diff --git a/TestMvvm/Views/MainWindow.axaml b/TestMvvm/Views/MainWindow.axaml index c249cbb..7bb8d9e 100644 --- a/TestMvvm/Views/MainWindow.axaml +++ b/TestMvvm/Views/MainWindow.axaml @@ -19,6 +19,7 @@ +