我需要为我的WPF应用程序创建一个MarkupExtension,它需要能够接受任意数量的参数,并用于国际化字符串中的令牌替换。我们的国际化代码使用params数组来获取这些参数。由于我希望这些参数可以从XAML传递,是否有一种方法可以在XAML中指定这些参数,而不必显式创建x:Array?
我需要为我的WPF应用程序创建一个MarkupExtension,它需要能够接受任意数量的参数,并用于国际化字符串中的令牌替换。我们的国际化代码使用params数组来获取这些参数。由于我希望这些参数可以从XAML传递,是否有一种方法可以在XAML中指定这些参数,而不必显式创建x:Array?
我认为你需要使用x:Array,我不知道其他的方法(也想不出其他能兼容标记扩展{}语法的方法)。
[MarkupExtensionReturnType(typeof(PriorityBinding))]
public sealed class Priority:MarkupExtension {
readonly BindingBase[] bindings;
#region Constructors
public Priority(BindingBase b1) {
bindings = new [] {b1};
}
public Priority(BindingBase b1, BindingBase b2) {
bindings = new [] {b1, b2};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3) {
bindings = new [] {b1, b2, b3};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
BindingBase b4) {
bindings = new [] {b1, b2, b3, b4};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
BindingBase b4, BindingBase b5) {
bindings = new [] {b1, b2, b3, b4, b5};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
BindingBase b4, BindingBase b5, BindingBase b6) {
bindings = new [] {b1, b2, b3, b4, b5, b6};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
BindingBase b4, BindingBase b5, BindingBase b6, BindingBase b7) {
bindings = new [] {b1, b2, b3, b4, b5, b6, b7};
}
public Priority(BindingBase b1, BindingBase b2, BindingBase b3,
BindingBase b4, BindingBase b5, BindingBase b6, BindingBase b7, BindingBase b8) {
bindings = new [] {b1, b2, b3, b4, b5, b6, b7, b8};
}
#endregion
public override object ProvideValue(IServiceProvider serviceProvider) {
var binding = new PriorityBinding();
foreach (var item in bindings) {
binding.Bindings.Add(item);
}
return binding;
}
}