c# - Windows Phone 8 绑定(bind)到具有格式的字符串资源

标签 c# xaml windows-phone-8

我的本​​地化资源字符串名为 TextResource,其值为:Text: {0}。其中 {0} 是 String.Format 的占位符。

我的用户控件有一个名为 Count 的 DependecyProperty。

我想将 Count 绑定(bind)到文本框的文本,但也应用本地化字符串。这样文本 block 的内容就是Text: 5(假设Count的值为5)

我想出了如何绑定(bind)本地化字符串

  <TextBlock Text="{Binding Path=LocalizedResources.TextResource, Source={StaticResource LocalizedStrings}}" />

或属性值

 <TextBlock Text="{Binding Path=Count}" />

但不是同时发生。

我如何在 XAML 中做到这一点?

PS:一种选择是添加两个文本 block 而不是一个,但我不确定这是否是一种好的做法。

最佳答案

这里有三个选项。

第一个选项:修改您的 View 模型以公开您的格式化字符串并绑定(bind)到它。

public string CountFormatted {
  get {
     return String.Format(AppResources.TextResource, Count);
  }
}
<TextBlock Text="{Binding Path=CountFormatted}" />

第二个选项:制作一个转换器MyCountConverter

public class MyCountConverter: IValueConverter {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    if (value == null)
      return value;

    return String.Format(culture, AppResources.TextResource, value);
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    throw new NotImplementedException();
  }
}
<phone:PhoneApplicationPage.Resources>
    <local:MyCountConverter x:Key="MyCountConverter"/>
</phone:PhoneApplicationPage.Resources>
...
<TextBlock Text="{Binding Count, Converter={StaticResource MyCountConverter}}"/>

第三个选项:使用可绑定(bind)的转换器参数,这样您就可以制作一个通用的 StringFormat 转换器,您可以在其中实际绑定(bind)转换器参数。这在 Windows Phone 中不支持开箱即用,但仍然可行。检查this有关如何完成的链接。

但是,除非您使用资源来支持多种语言,否则将您的格式作为纯字符串传递给转换器会容易得多。

<TextBlock Text="{Binding Count, 
                  Converter={StaticResource StringFormatConverter}, 
                  ConverterParameter='Text: {0}'}" />

在这种情况下,您必须制作一个使用该参数的 StringFormatConverter 转换器。

编辑:

关于第三个选项,您可以使用上面链接中的IMultiValueConverter 来实现您想要的。您可以添加以下转换器:

public class StringFormatConverter: IMultiValueConverter {
  public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    var param = values[0].ToString();
    var format = values[1].ToString();

    return String.Format(culture, format, param);
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
    throw new NotImplementedException();
  }
}
<TextBlock Text="{Binding ElementName=MultiBinder, Path=Output}" />

<binding:MultiBinding x:Name="MultiBinder" Converter="{StaticResource StringFormatConverter}"
    NumberOfInputs="2"
    Input1="{Binding Path=Count, Mode=TwoWay}"
    Input2="{Binding Path=LocalizedResources.TextResource, Source={StaticResource LocalizedStrings}}" />

虽然我不知道这是否值得付出努力。

关于c# - Windows Phone 8 绑定(bind)到具有格式的字符串资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16642830/

相关文章:

c# - 如何基于泛型类型创建泛型实例

C# 相当于 SQL Server 中的 IsNull() 函数

c# - Xamarin XAML : Placing a Grid over an Image (responsive to screen size)

windows-phone-8 - Windows 8 手机模拟器方向更改按钮不会更改或触发 "OnOrientationChanged"

c# - 将动态 DataTable 转换为 List<Dictionary<string,string>>

c# - 佳能远程 SDK 替代品?

wpf - 当转换器位于 UserControl.Resources 中时,XAML 设计器 "cannot find type"

wpf - WPF在XAML中旋转矩形动画

c# - 处理 Windows Phone 8 silverlight 中的客户端 SSL 证书错误

c# - WP8错误媒体播放器无法启动