【问题标题】:Windows Phone 8 bind to string resource with formatWindows Phone 8 使用格式绑定到字符串资源
【发布时间】:2013-05-20 04:54:01
【问题描述】:

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

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

我想将Count 绑定到文本框的文本,但也应用本地化字符串。使得文本块的内容为Text: 5(假设Count的值为5)

我设法弄清楚如何绑定本地化字符串

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

或属性值

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

但不是两者同时发生。

如何在 XAML 中做到这一点?

PS:一种选择是添加两个文本块而不是一个,但我不确定这是否是一个好习惯。

【问题讨论】:

    标签: c# xaml windows-phone-8


    【解决方案1】:

    这里有三个选项。

    第一个选项:修改您的视图模型以公开您的格式化字符串并绑定到该字符串。

    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}}"/>
    

    第三个选项:使用可绑定转换器参数,这样您就可以制作一个通用的 StringFormat 转换器,您可以在其中实际绑定转换器参数。 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}}" />
    

    我不知道这是否值得。

    【讨论】:

    • 非常感谢!选项#3 是否可以将 ConverterParameter 绑定到资源文件中的资源?我试过了,它失败了......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2014-01-15
    • 2013-12-30
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多