2017-05-03 1 views
1

Je tente de modifier mon texte lié à un fichier de ressources en cours d'exécution.Modification du texte lié à un fichier de ressources

Pour que je suivais ce tutoriel:

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

J'ai simplement créé un projet WPF, a ajouté 2 fichier de ressources, le premier nom Resource.resx et le second nommé Resource.pt-PT .resx, les deux ont un champ appelé Tag1. Ensuite, j'ai créé une classe avec le code donné dans le tutoriel précédent:

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; 
     } 
    } 
} 

Et enfin créé une interface simple avec le code XAML suivant:

<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> 

C'est ce que mon événement Button_Click fait:

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

mon problème actuel se connecte le code TranslationSource avec mon interface utilisateur, le tutoriel manque cette partie, sans doute parce que son quelque chose de très simple, mais je unfortunatelly Je ne suis pas très expert en matière de WPF ... Quelqu'un peut-il m'expliquer quelles sont les prochaines étapes?

Répondre

0

TranslationSource n'est pas un espace de noms, il est un nom de classe

de changement

xmlns:ns="clr-namespace:TranslationSource" 

à

xmlns:ns="clr-namespace:WpfApplication1" 

ou l'espace de noms approprié où vous avez les classes TranslationSource et LocExtension