C# 在XAML中使用静态函数绑定
我有一个XAML文件,我想用变量替换字段名,这样我就可以为我的应用程序提供更多语言的翻译支持 我目前在应用程序中使用翻译的方式类似于translation.getString(“stringname”)(translation是一个静态类,带有静态getString函数,它从资源文件中获取翻译后的字符串) 现在对于xaml部分,我有如下内容:C# 在XAML中使用静态函数绑定,c#,wpf,xaml,.net-4.5,C#,Wpf,Xaml,.net 4.5,我有一个XAML文件,我想用变量替换字段名,这样我就可以为我的应用程序提供更多语言的翻译支持 我目前在应用程序中使用翻译的方式类似于translation.getString(“stringname”)(translation是一个静态类,带有静态getString函数,它从资源文件中获取翻译后的字符串) 现在对于xaml部分,我有如下内容: <TextBlock Text="Some string for translation" VerticalAlignment="Center"/&
<TextBlock Text="Some string for translation" VerticalAlignment="Center"/>
<TextBlock Text="{local:TranslateString Value='Some string for translation'}"/>
xmlns:local="clr-namespace:WpfApplication1"
Xaml:
但这是相当丑陋和冗长的。我可以直接从xaml访问函数吗
例如:
<TextBlock Text="{CustomBinding stringname}" VerticalAlignment="Center"/>
在我的项目中的某个地方,我指定使用“CustomBinding bla”将其作为函数Translation.getString(“bla”)运行,并使用返回的字符串获得我想要的翻译效果
这有可能吗
是否有其他的“业务标准”通常是这样做的?您可以通过创建自定义的
标记扩展来实现这一点,并使用它代替绑定
public class TranslateString : MarkupExtension
{
public string Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (String.IsNullOrWhiteSpace(Value))
return Value;
return Translation.getString(Value);
}
}
现在,您可以从XAML使用它,如下所示:
<TextBlock Text="Some string for translation" VerticalAlignment="Center"/>
<TextBlock Text="{local:TranslateString Value='Some string for translation'}"/>
xmlns:local="clr-namespace:WpfApplication1"
将WpfApplication1
替换为类TranslateString的实际名称空间。据我所知,没有针对此类问题的业务标准
如果说XAML有什么是真的,那就是它太冗长了。这并不总是一件坏事,但这意味着即使是具有简单自定义行为的控件也可能很棘手
有一个,但它是复杂的,我不骗你,实际上包括使用和修改microsoft提供的示例应用程序作为工作流的一部分的说明
另一种选择是按照通常的方式进行开发,并代理本地化。当然,这很容易,但要花钱
使用任一工作流,您都可以在应用程序中定义字符串。资源
并将其引用为动态资源
我实现了如下功能:
public partial class App : Application
{
Dictionary<string, string> localizer;
public App()
{
localizer = new Dictionary<string, string>() { { "loc", "locdsasdasd" } };
//LoadLocalizer();
this.Resources.Add("Localizer", localizer);
}
}
公共部分类应用程序:应用程序
{
字典定位器;
公共应用程序()
{
localizer=新字典(){{“loc”,“locdsasdasd”};
//LoadLocalizer();
添加(“定位器”,定位器);
}
}
LocBinding.cs
using System;
using System.Windows.Data;
using System.Collections.Generic;
namespace Stackoverflow
{
public class LocBinding : Binding
{
static Dictionary<string, string> localizer=((Dictionary<string,string>)
(App.Current.Resources["Localizer"]));
public LocBinding(string property)
{
if (string.IsNullOrWhiteSpace(property))
throw new ArgumentNullException("binding path is null");
string value;
if (localizer.TryGetValue(property, out value))
this.Source = value;
else
throw new KeyNotFoundException(property + "key is not defined in
localizer");
}
}
}
<Window x:Class="Stackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stackoverflow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="{local:LocBinding loc}"/>
</Grid>
使用系统;
使用System.Windows.Data;
使用System.Collections.Generic;
命名空间堆栈溢出
{
公共类绑定:绑定
{
静态字典定位器=((字典)
(App.Current.Resources[“定位器]);
公共绑定(字符串属性)
{
if(string.IsNullOrWhiteSpace(属性))
抛出新ArgumentNullException(“绑定路径为null”);
字符串值;
if(定位器.TryGetValue(属性,输出值))
来源=价值;
其他的
throw new KeyNotFoundException(属性+)中未定义键
定位器);
}
}
}
.xaml.cs
using System;
using System.Windows.Data;
using System.Collections.Generic;
namespace Stackoverflow
{
public class LocBinding : Binding
{
static Dictionary<string, string> localizer=((Dictionary<string,string>)
(App.Current.Resources["Localizer"]));
public LocBinding(string property)
{
if (string.IsNullOrWhiteSpace(property))
throw new ArgumentNullException("binding path is null");
string value;
if (localizer.TryGetValue(property, out value))
this.Source = value;
else
throw new KeyNotFoundException(property + "key is not defined in
localizer");
}
}
}
<Window x:Class="Stackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stackoverflow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="{local:LocBinding loc}"/>
</Grid>
localizer中的这一个值仅用于测试。但实际上,这些值将从数据库、xml或.resx文件中填充。我将这两个值与接受的答案结合在一起,它完全按照我想要的方式工作,完美无瑕。