更改绑定到资源文件的文本

时间:2017-05-03 20:56:48

标签: c# wpf localization bind

我正在尝试在运行时将文本绑定到资源文件。

为此,我正在学习本教程:

https://codinginfinity.me/post/2015-05-10/localization_of_a_wpf_app_the_simple_approach

我只创建了一个WPF项目,添加了2个资源文件,第一个名为Resource.resx,第二个名为Resource.pt-PT.resx,它们都有一个名为Tag1的字段。然后,我使用上一个教程中给出的代码创建了一个类:

namespace WpfApplication1
{
    public class TranslationSource
            : INotifyPropertyChanged
    {
        private static readonly TranslationSource instance = new TranslationSource();

        public static TranslationSource Instance
        {
            get { return instance; }
        }

        private readonly ResourceManager resManager = Properties.Resources.ResourceManager;
        private CultureInfo currentCulture = null;

        public string this[string key]
        {
            get { return this.resManager.GetString(key, this.currentCulture); }
        }

        public CultureInfo CurrentCulture
        {
            get { return this.currentCulture; }
            set
            {
                if (this.currentCulture != value)
                {
                    this.currentCulture = value;
                    var @event = this.PropertyChanged;
                    if (@event != null)
                    {
                        @event.Invoke(this, new PropertyChangedEventArgs(string.Empty));
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class LocExtension
        : Binding
    {
        public LocExtension(string name)
            : base("[" + name + "]")
        {
            this.Mode = BindingMode.OneWay;
            this.Source = TranslationSource.Instance;
        }
    }
}

最后使用以下XAML代码创建了一个简单的界面:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ns="clr-namespace:TranslationSource"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Margin="10">
            <Button Content="{x:Static ns:Loc Tag1}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Window>

这就是我的Button_Click事件:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TranslationSource.Instance.CurrentCulture = new System.Globalization.CultureInfo("pt-PT");
    }

我目前的问题是将TranslationSource代码与我的UI连接,教程错过了那部分,可能是因为它非常简单,但不幸的是我不是WPF的专家......有人能解释一下接下来的步骤是什么吗?

1 个答案:

答案 0 :(得分:0)

#creating an EMI vector em=c(10000,20000,30000,40000,50000,60000,70000,80000,90000,100000) #my dataframe where i want to store predictions v<-c() v <- data.frame(v) newdata$EMI.Amount<-em prediction=predict(rf,newdata,type="response") kl <- table(prediction) v <- rbind(v,kl) 不是名称空间,而是类名

变化

TranslationSource

xmlns:ns="clr-namespace:TranslationSource"

或您拥有xmlns:ns="clr-namespace:WpfApplication1" TranslationSource

的相应命名空间