Hey everyone,
I try to implement https://forums.xamarin.com/discussion/82458/binding-indexername-and-binding-providevalue-in-xamarin-forms and I got it. Its working with one label.
My issue is with labels from the listview in my MasterDetail page , the labels doesn't change, if someone see what kind of issue, really thanks you.
My View
<ListView Grid.Row="1" x:Name="MenuListPage" SeparatorVisibility="None"
HasUnevenRows="true" ItemsSource="{Binding MenuItems}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
Text="{Binding Title}"
FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When I Change language
public static void LoadNewLanguageApp(object language)
{
var culture = GetCultureInfoFromString(language);
// Update UI
DependencyService.Get<ILanguage>().SetLocale(culture);
Translator.TranslatorInstance.Invalidate();
}
My Translator class
/// <summary>
/// Class using singleton design pattern to translate text
/// </summary>
public class Translator : INotifyPropertyChanged
{
// Path of folder where is store each file language + Name of file without .en ( language code )
private const string ResourceId = "Landauer.Mobile.Ressources.Language.LanguageRessource";
/// <summary>
/// Current culture info
/// </summary>
private static CultureInfo CultureInfoApp { get; set; }
/// <summary>
/// Unique instance of translator
/// </summary>
private static readonly Translator _uniqueInstance = new Translator();
/// <summary>
/// Public instance of Translator
/// </summary>
public static Translator TranslatorInstance => _uniqueInstance;
// Instanciation differed of Ressourcemanager
public static readonly Lazy<ResourceManager> RessourceManagerLanguage = new Lazy<ResourceManager>(() => new ResourceManager(ResourceId, IntrospectionExtensions.GetTypeInfo(typeof(TranslateExtension)).Assembly));
/// <summary>
/// When TranslateExtension you create new Binding(), call this "Callback"
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public string this[string text]
{
get
{
return RessourceManagerLanguage.Value.GetString(text, CultureInfoApp);
}
}
private void InitCulture()
{
if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
// Get dependency in each specific platform
CultureInfoApp = DependencyService.Get<ILanguage>().GetCurrentCultureInfo();
}
}
/// <summary>
/// Implementation of notifications
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// At each time you set language use this method to refresh UI
/// </summary>
public void Invalidate()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}
}
My TranslateExtension
// Exclude suffix 'Extension', like that in XAML use Translate
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension<BindingBase>
{
/// <summary>
/// Match to the name of the label into .resx file
/// </summary>
public string Text { get; set; }
public TranslateExtension()
{
}
public BindingBase ProvideValue(IServiceProvider serviceProvider)
{
var binding = new Binding
{
Mode = BindingMode.OneWay,
Path = $"[{Text}]",
Source = Translator.TranslatorInstance,
};
return binding;
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
}
When I Bind Label of my listview
/// <summary>
/// List of items available into the main menu
/// </summary>
public ObservableCollection<MainMenuViewItem> MenuItems
{
get
{
return this._menuItems;
}
set
{
this._menuItems = value; RaisePropertyChanged(() => MenuItems);
}
}
private ObservableCollection<MainMenuViewItem> SetDatas()
{
return new ObservableCollection<MainMenuViewItem>
{
new MainMenuViewItem {Title = Translator.TranslatorInstance["lblHome"], TargetType=typeof(HybridWebView),ViewModelType=HybridViewTypeEnum.Home },
new MainMenuViewItem {Title = Translator.TranslatorInstance["lblListOfDeliverySlip"], TargetType=typeof(HybridWebView),ViewModelType=HybridViewTypeEnum.ListDeliverySlip },
new MainMenuViewItem { Title = Translator.TranslatorInstance["lblSettings"], TargetType=typeof(ParametersView)}
};
}
}
/// <summary>
/// Model of items for MainMenuMasterView
/// </summary>
public class MainMenuViewItem : INotifyPropertyChanged
{
private string _title;
private HybridViewTypeEnum _viewModelType;
private Type _targetType;
public MainMenuViewItem() { }
public string Title
{
get { return this._title; }
set { this._title=value; OnPropertyChanged(); }
}
public HybridViewTypeEnum ViewModelType
{
get { return this._viewModelType; }
set { this._viewModelType = value; OnPropertyChanged(); }
}
public Type TargetType
{
get { return this._targetType; }
set { this._targetType =value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}