的Windows Phone 8绑定与格式字符串资源 [英] Windows Phone 8 bind to string resource with format

查看:172
本文介绍了的Windows Phone 8绑定与格式字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的用户控件有一个名为DependecyProperty 计数



我要绑定计数来的文本一个文本框,但也适用于本地化的字符串。使文本块的内容正文:5 (假设的值计数值为5)



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

 <的TextBlock文本={绑定路径= LocalizedResources.TextResource源= {StaticResource的LocalizedStrings}}/> 

或属性值

 < TextBlock的文本={绑定路径= COUNT}/> 



但不是两者同时进行。



我怎样才能做到在XAML



PS:一个办法是增加两个文本块,而不是一但我不知道这是一个很好的做法。


解决方案

您这里有三个选项。



首选项:修改您的视图模型暴露你的格式化字符串并绑定到



<预类= 郎咸平-CS prettyprint-覆盖> 公共字符串CountFormatted {
获得{
返回的String.Format(AppResources.TextResource,计数);
}
}



<预类=郎咸平的XML prettyprint-覆盖> < TextBlock的文本={绑定路径= CountFormatted}/>



第二个选项:做一个转换器 MyCountConverter



<预类=郎-CS prettyprint-覆盖> 公共类MyCountConverter:的IValueConverter {
公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
如果(价值== NULL)
返回值;

返回的String.Format(文化,AppResources.TextResource,价值);
}

公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
抛出新NotImplementedException();
}
}



<预类=郎咸平的XML prettyprint-覆盖> <电话:PhoneApplicationPage.Resources>
<局部:MyCountConverter X:键=MyCountConverter/>
< /电话:PhoneApplicationPage.Resources>

< TextBlock的文本={结合计数,转换器= {StaticResource的MyCountConverter}}/>



第三个选项:使用绑定,能够转换参数,这样就可以使一般的StringFormat转换器,你实际上可以绑定变频器参数。这是不支持出的windows phone的盒子,但仍是可行的。检查它如何能够做到这链接。



然而,除非你正在使用的资源来支持多种语言那么它更容易只是通过您的格式为纯字符串转换器。



<前类=郎咸平的XML prettyprint-覆盖> < TextBlock的文本={结合计数,
转换器= {StaticResource的StringFormatConverter}
ConverterParameter ='文字:{0 }'}/>

您将不得不作出一个 StringFormatConverter 。使用在这种情况下,参数转换器



修改



关于第三选项,您可以使用 IMultiValueConverter 在上面的链接,以达到你想要的东西。您可以添加以下转换器:



<预类=郎-CS prettyprint-覆盖> 公共类StringFormatConverter:IMultiValueConverter {
公共对象转换(对象[]值类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
VAR参数=值[0]的ToString();
VAR格式=值[1]的ToString();

返回的String.Format(文化,格式,参数);
}

公共对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,System.Globalization.CultureInfo文化){
抛出新NotImplementedException();
}
}



<预类=郎XAML prettyprint-覆盖> < TextBlock的文本={绑定的ElementName = MultiBinder,路径=输出}/>

<结合:MultiBinding X:NAME =MultiBinder转换器={StaticResource的StringFormatConverter}
NumberOfInputs =2
输入1 ={绑定路径=计数,模式=双向}
输入2 ={绑定路径= LocalizedResources.TextResource源= {StaticResource的LocalizedStrings}}/>



我不知道这是否是值得的,虽然。


My localized resource string, named TextResource has the value: Text: {0}. Where {0} is the placeholder for String.Format.

My user control has a DependecyProperty called Count.

I would like to bind Count to the text of a text box but also apply the localized string. So that the content of the text block is Text: 5 (assuming the value of Count is 5)

I managed to figure out how to bind the localized string

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

or the property value

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

but not both simultaneous.

How can I do that in XAML?

PS: One option would be to add two text blocks instead of one but I am not sure if that is a good practice.

解决方案

You have three options here.

First option: Modify your view model to expose your formatted string and bind to that.

public string CountFormatted {
  get {
     return String.Format(AppResources.TextResource, Count);
  }
}

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

Second option: Make a converter 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}}"/>

Third option: Use bind-able converter parameter so that you can make a general StringFormat converter where you can actually bind the converter parameter. This is not supported out of the box in windows phone but is still doable. Check this link on how it can be done.

However, unless you are using resources to support multiple languages then it's much easier to just pass your format as a plain string to a converter.

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

You'll have to make a StringFormatConverter converter that uses the parameter in this case.

Edit:

Regarding third option, you can use the IMultiValueConverter in the link above to achieve what you want. You can add the following converter:

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

I don't know if it's worth the effort though.

这篇关于的Windows Phone 8绑定与格式字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆