1

I'm making an Android app in Xamarin using MvvmCross.

My UI has a search field and a button. I want the button to reflect whatever is in the search field. So if the search field has Alamo Drafthouse, I want the button to say Create "Alamo Drafthouse" Venue, however the Create "x" Venue string should come from the strings.xml file so that it can be localized.

My current plan of attack is to create a ValueConverter that takes the search term and does the formatting, using the ConvertParameter to specify the ID of the string resource, so I'd be able to do something like this:

<Button local:MvxBind="Text SearchTerm, Converter=FormatResource, ConvertParameter='create_view'" />

where create_view is a string resource defined in strings.xml.

I thought I should ask whether there is a better way to do this. Is there?

Uchendu Nwachuku
  • 442
  • 3
  • 13

1 Answers1

1

If you want the template string to come from strings.xml (from Android project, not from the Core/PCL project), you may create your own converter named FormatResourceConverter.

 public class FormatResourceConverter: IMvxValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         //value would be your draft house
         //parameter would be a name of your string in strings.xml
         var stringValue = ... //get your string from Android string resources 
         return String.Format(stringValue, value);    
     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

Here is a simple answer how to get string from Android resources in Xamarin.

Community
  • 1
  • 1
Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52