Skip to content

How to binding general string indexers? #10033

Open
@RedTellYou

Description

I have a general language change singleton class

public class LanguageManager : INotifyPropertyChanged
{
private readonly ResourceManager _resourceManager;
private static readonly Lazy Lazy = new(() => new LanguageManager());
public static LanguageManager Instance => Lazy.Value;

private LanguageManager()
{
    _resourceManager = new ResourceManager(typeof(Resource));
}

public string this[string name]
{
    get
    {
        if (name == null)
        {
            throw new ArgumentNullException(nameof(name));
        }
        return _resourceManager.GetString(name)!;
    }
}

public void ChangeLanguage(string languageName)
{
    var culture = CultureInfo.GetCultureInfo(languageName);
    CultureInfo.CurrentCulture = culture;
    CultureInfo.CurrentUICulture = culture;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));
}

public event PropertyChangedEventHandler? PropertyChanged;

}

In WPF you can do this:

<TextBlockText="{Binding [Welcome], Source={x:Static localization:LanguageManager.Instance}}" />

But this cannot be done in WinUI3:
<TextBlock Text="{Binding [Welcome], Source={x:Static localization:LanguageManager.Instance}}" />

  1. No x:static
  2. Binding needs to contain a public constructor
  3. Can not work If I switch to x:bind:
  4. Is this question related to mine?[([Feature] x:Bind should support general string indexers without interface in C# #3064)]

So, In winui3, How to do?

Metadata

Assignees

No one assigned

    Labels

    feature proposalNew feature proposalneeds-triageIssue needs to be triaged by the area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions