Hi all,
I use a ResourceManager with an indexer in my ViewModels for hot reload translation. As I have an ObservableCollection of strings that is hard-coded in the ViewModel, I want to use a converter to translate this part of my app:
# The view code-behind
public partial class ScoreListPage : ViewPageBase
{
protected ScoreListViewModel ViewModel { get; } = new ScoreListViewModel();
public ScoreListPage()
{
InitializeComponent();
this.BindingContext = ViewModel;
this.Resources.Add("TranslateConverter", new TranslateConverter(ViewModel.ScoreResources));
}
}
public class TranslateConverter : IValueConverter
{
public IResourceProvider ResourceProvider { get; private set; }
public TranslateConverter(IResourceProvider resourceProvider)
{
ResourceProvider = resourceProvider;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ResourceProvider[value.ToString()];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
# The ViewModel
public class ScoreListViewModel : ViewModelBase
{
public ObservableCollection<Category> Categories { get; set; }
public ScoreListViewModel()
{
this.Categories = new ObservableCollection<Category>
{
new Category ("HeartFailure")
{
new Score("PhhfGroup", "PhhfGroup_DisplayName", "PhhfGroup_Detail"),
new Score("PhhfGroup", "PhhfGroup_DisplayName", "PhhfGroup_Detail")
},
new Category ("Rythmology")
{
new Score("ChadsVasc", "BMI_DisplayName", "ChadsVasc_Detail"),
new Score("HasBled", "PhhfGroup_DisplayName", "HasBled_Detail")
}
};
}
}
And the XAML:
<ListView
ItemsSource="{Binding Categories}"
GroupDisplayBinding="{Binding Name}"
IsGroupingEnabled="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName, Converter={StaticResource TranslateConverter}}"
Detail="{Binding Detail, Converter={StaticResource TranslateConverter}}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
But the translation does not work properly as it needs a restart. Why is this not functional ?
Thanks