xamarin form - label stringformat in resourcefile

ManhaoChenManhaoChen NZMember ✭✭
edited October 2016 in Xamarin.Forms

is it possible to have the format string in a resource file like the following?

<Label x:Name="lblName" Text="{Binding GivenName, StringFormat='x:Static resx:AppResources.lbl_format'}" Style="{StaticResource PersonalDetailsLblName}" />

resx/AppResources.resx
<data name="lbl_format" xml:space="preserve"> <value>Hi {0}</value> </data>
my expected label text is 'Hi '

but what i get when running emulator the label text = x:Static resx:AppResources.lbl_format

Best Answers

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Yes you can put string formatting as a resource. I do it as follows (NOT in the app.resx though, but it should be basically the same):

    On the page:

                            <Label BindingContext="{x:Reference this}"
                                   Text="{Binding GpsLastValid.Latitude,
                                                  StringFormat={StaticResource formatdecimalfourplaces}}">
    

    then the resource as:

                <x:String x:Key="formatdecimalfourplaces">{0:0.0000}</x:String>
    
  • ManhaoChenManhaoChen NZMember ✭✭

    ah, the reason i wanting to put it into resource is due to globalization

    there will be some instances where i'll have to use eg $100 vs ¥100 (this is just an example)

    resx/AppResources.resx
    <data name="lbl_currency_format" xml:space="preserve"> <value>${0}</value> </data>

    resx/AppResources.zh.resx
    <data name="lbl_currency_format" xml:space="preserve"> <value>¥{0}</value> </data>

Sign In or Register to comment.