WPF本地化扩展MVVM绑定

3
我正在尝试绑定在ViewModel中声明的名为["SampleName"]的属性。这里的SampleName是关键字。然而,当尝试以下代码时,它显示为空。
<TextBlock FontSize="14" Text="{lex:BLoc Value=SampleName}" lex:ResxLocalizationProvider.DefaultAssembly="SAS.Resources" lex:ResxLocalizationProvider.DefaultDictionary="Report" HorizontalAlignment="Center" VerticalAlignment="Top" TextDecorations="Underline" FontWeight="DemiBold" Grid.Row="0" Grid.ColumnSpan="{Binding Path=SpanCount}" Grid.Column="0"/>

请为我提供建议。

我也遇到了同样的问题。你找到任何解决方案了吗? - bruno.almeida
2个回答

2

我之前使用了推荐的答案,但那不是100%稳定和线程安全的。现在我改用这种解决方案。

我的看法:

xmlns:lex="http://wpflocalizeextension.codeplex.com"

<TextBlock Text="{Binding ElementName=LocProxy, Path=Result}"/>
<lex:LocProxy Name="LocProxy"
           Source="{Binding NameOfResourceKey}"/>

我的视图模型:

public string NameOfResourceKey { get; set; }

然后在ViewModel中像这样设置:

// Use your way to access classis Resources.resx file
NameOfResourceKey = nameof(Properties.Resources.YourLocalizedMessageKey);
// Use your function to RaiseProperty     
RaisePropertyChanged(nameof(NameOfResourceKey)); 

所以,您需要通过nameof在资源文件中引用您的资源,将其设置为lex:LocProxy的源,然后在需要本地化字符串的任何位置绑定到LoxProxy Result。非常简单,然后GUI将处理所有本地化,这是最佳选择。

对于提高属性,您可以使用bruno.almeida的函数(OnPropertyChanged),或者例如使用MVVMLight nuget包并在ViewModel中继承ViewModelBase,就像我一样。


1
我查看了源代码,不认为BLoc是用来做那个的。但我找到了实现相同结果的其他方法。
基于this的回答,我想出了这个解决方案。不要将资源键提供给视图,而是提供资源值。如果文化发生变化,则该值也会发生变化。这是核心实现:
public class MyViewModel : INotifyPropertyChanged {

    public MyViewModel()
    {
        // Properties.Resources is my Resources.resx file and String1 is my resource key
        BindPropertyToResource(nameof(Name), nameof(Properties.Resources.String1));
    }


    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (_name != value)
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    }

    protected void BindPropertyToResource(string propertyName, string resourceKey){
        WPFLocalizeExtension.Providers.ResxLocalizationProvider resxLocalizationProvider
            = WPFLocalizeExtension.Providers.ResxLocalizationProvider.Instance;

        WPFLocalizeExtension.Providers.ResxLocalizationProvider.SetDefaultAssembly(resxLocalizationProvider, System.Reflection.Assembly.GetCallingAssembly().GetName().Name);
        WPFLocalizeExtension.Providers.ResxLocalizationProvider.SetDefaultDictionary(resxLocalizationProvider, nameof(Properties.Resources));

        var targetProperty = this.GetType().GetProperty(propertyName);
        var locBinding = new WPFLocalizeExtension.Extensions.LocTextExtension(resourceKey);

        locBinding.SetBinding(this, targetProperty);
    }


    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion // INotifyPropertyChanged Members
}

希望这个解决方法能够解决您的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接