I use IValueConverters a lot in my Xamarin projects. I would like to use any arbitrary string of text as the binding parameter, rather than have to bind to a property on my View or ViewModel. Often I find myself having to create what I see as arbitrary / superfluous properties on my view model just to support the functionality I want, which clutters my code. The use case for this is using a converter to get a translated string from a custom XML file. (In my project I cannot use the standard resx file i18n approach - I need to traverse an XML file to find the string I need).
I would like to do this in Xaml:
<Label Text="{Binding 'my-awesome-string-of-text',
Converter={StaticResource ConvertMyStringToSomethingElseConvertor}}"/>
and to have the string 'my-awesome-string-of-text' flow through to the object parameter of the IValueConvertor (ConvertMyStringToSomethingElseConvertor in this example).
But I find myself having to do this instead
<Label Text="{Binding MyUneccessaryStringProperty,
Converter={StaticResource ConvertMyStringToSomethingElseConvertor}}"/>
and on my view model (mvvm light syntax) having to maintain the property
private string _myUneccessaryStringProperty
public string MyUneccessaryStringProperty
{
get => _myUneccessaryStringProperty ;
set => SetProperty(ref _myUneccessaryStringProperty , value);
}
Can what I want be done? For a page with a lot of different translatable strings the ViewModel quickly becomes cluttered.
EDIT! This can be done with an IMarkupExtension!
It has been implemented in the Xamarin Commmunity toolkit for getting translated strings from RESX files in the TranslateExtension. I have adapted their approach to pull from my custom translations XML file and bind to that, something like this:
Then in the xaml for example the Text property of a label
The string displayed will be either the translation if it was successful, or the resource string if not, for debugging.
Original answer, for posterity
I found an acceptable answer, relating to Andrew's suggestion in the comments.
Basically I create a static class called translations and set static properties to the value of the translations in the way my app gets them.
And then reference in xaml like so:
Using the get/set accessors ensure that the GetTranslation call will only run once, effectively giving me efficient in memory dictionary-like access to resources that are used across multiple screens.
I still need to actually have the properties in a class, which is not ideal - but at least this way they are all in one place and not cluttering the viewmodels. If anyone knows a solution where I don't need to keep actual properties please let me know!