Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to binding general string indexers? #10033

Open
RedTellYou opened this issue Oct 3, 2024 · 5 comments
Open

How to binding general string indexers? #10033

RedTellYou opened this issue Oct 3, 2024 · 5 comments
Labels
feature proposal New feature proposal needs-triage Issue needs to be triaged by the area owners

Comments

@RedTellYou
Copy link

RedTellYou commented Oct 3, 2024

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?

@RedTellYou RedTellYou added the feature proposal New feature proposal label Oct 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Oct 3, 2024
@whiskhub
Copy link
Contributor

whiskhub commented Oct 3, 2024

@RedTellYou
Copy link
Author

也许标记扩展可以满足您的需求:https://devblogs.microsoft.com/ifdef-windows/use-a-custom-resource-markup-extension-to-succeed-at-ui-string-globalization/

This article does not seem to implement the code for dynamically switching languages

@AndrewKeepCoding
Copy link
Contributor

@RedTellYou Not an answer for you question but if you want to dynamically switch languages, try the WinUI3Localizer (Diclosure: I'm the author.).

@RedTellYou
Copy link
Author

RedTellYou commented Oct 9, 2024

@whiskhub @AndrewKeepCoding My project requires WPF and WinUI3 to share MVVM, so I can only consider how to achieve multi-language compatibility. The methods you provide can only be used in WinUI3, not in WPF.

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-triage Issue needs to be triaged by the area owners and removed needs-triage Issue needs to be triaged by the area owners labels Oct 9, 2024
@RedTellYou RedTellYou reopened this Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature proposal New feature proposal needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

3 participants