2017-09-06 16 views
-1

私のアプリケーションで複数の言語を使用しています。
MVVMパターンでこれをやろうとしていますので、 私はCodeProjectとTutorialspointからチュートリアルを読みます。ViewModelのモデルプロパティへのバインディング[MVVM]

HERESに私のプロジェクトの構造:次に、私は私の中でそのモデルのインスタンスを作成

internal class LanguageModel : INotifyPropertyChanged 
{ 
    //GUI Language 

    private string title; 
    private string inputBtn; 
    private string outputBtn; 
    private string convertBtn; 
    ... 
    public string Title 
    { 
     get 
     { 
      return title; 
     } 

     set 
     { 
      if(title != value) 
      { 
       title = value; 
       RaisePropertyChanged("Title"); 
      } 

     } 
    } 


    public string InputBtn 
    { 
     get 
     { 
      return inputBtn; 
     } 

     set 
     { 
      if (inputBtn != value) 
      { 
       inputBtn = value; 
       RaisePropertyChanged("InputBtn"); 
      } 
     } 
    } 

    public string OutputBtn 
    { 
     get 
     { 
      return outputBtn; 
     } 

     set 
     { 
      if (outputBtn != value) 
      { 
       outputBtn = value; 
       RaisePropertyChanged("OutputBtn"); 
      } 
     } 
    } 

    public string ConvertBtn 
    { 
     get 
     { 
      return convertBtn; 
     } 

     set 
     { 
      if (convertBtn != value) 
      { 
       convertBtn = value; 
       RaisePropertyChanged("ConvertBtn"); 
      } 
     } 
    } 

    ... 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string property) 
    { 
     if(PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

}

Model 
ConverterModel.cs 
LanguageModel.cs 
ViewModel 
ConverterViewModel.cs 
View 
ConverterView.xaml 
    ConverterView.xaml.cs 
MainWindow.xaml 
MainWindow.xaml.cs 

私は私が必要とするすべての言語の文字列のためのモデルを作成しましたViewModelを開き、目的のデータで埋めてください。

internal class ConverterViewModel 
{ 
    internal Settings Settings{ get; set; } 
    ConverterModel Model { get; set; } //where the Data Fill Method is located 
    internal LanguageModel LanguageModel { get; set; } 
    String Title {get; set;} = "Banana"; //<-- this seems to work just fine 

    internal ConverterViewModel() 
    { 
     Model = new ConverterModel(); 
     Settings = new Settings(); 
     if(Settings.Language == "") 
     { 
      Settings.Language = "English"; 
      //getLanguage 
      Settings.Save(); 
     } 
     this.LanguageModel = Model.SetLanguageModel(Settings.Language); 
    } 

データがConverterModel方法を入力します。

class ConverterModel 
{ 
      internal LanguageModel SetLanguageModel(string language) 
    { 
     switch (language) 
     { 
      case "English": 
       LanguageModel english = new LanguageModel() 
       { 
        Title = "TitleSomething", 
        InputBtn = "Inputfile", 
        OutputBtn = "Outputfile", 
        ConvertBtn = "Convert", 
       }; 
       return english; 
      case "German": 
       LanguageModel german = new LanguageModel() 
       { 
        //Work in Progress 
       }; 
       return german; 
      case "French": 
       LanguageModel french = new LanguageModel() 
       { 
        //Work in Progress 
       }; 
       return french; 
      case "Italian": 
       LanguageModel italian = new LanguageModel() 
       { 
       //Work in Progress 
       }; 
       return italian; 
      default: return null; 
     } 

    } 
} 

私は私のバインディングは次のように設定されています。

メインウィンドウ:

<Window x:Class="dta2pain.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:dta2pain" 
    xmlns:view="clr-namespace:dta2pain.View" 
    xmlns:viewModel="clr-namespace:dta2pain.ViewModel" 
    mc:Ignorable="d" 
    ResizeMode="NoResize" 
    WindowStartupLocation="CenterScreen" 
    Title="{Binding Title}" //<-- works 
    Height="657" Width="910"> 
<Grid> 
<Window.DataContext> 
    <viewModel:ConverterViewModel/> 
</Window.DataContext> 
    <view:ConverterView x:Name="ConverterViewControl"> 
     <view:ConverterView.DataContext> 
      <viewModel:ConverterViewModel/> 
     </view:ConverterView.DataContext> 
    </view:ConverterView> 
</Grid> 

ConverterView:

<UserControl x:Class="dta2pain.View.ConverterView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:dta2pain.View" 
     xmlns:viewModel="clr-namespace:dta2pain.ViewModel" 
     mc:Ignorable="d" 
     d:DesignHeight="657" d:DesignWidth="910" 
     Title = "{Binding Path = Title}"> 
<Grid> 
    <Menu HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="910" > 

    </Menu> 
    <Grid Margin="0,15,0,0"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="0.5*" /> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="0.75*"/> 
      <RowDefinition Height="1.5*"/> 
      <RowDefinition Height="0.25*"/> 
     </Grid.RowDefinitions> 
     <Image x:Name="LogoImg" Grid.Row="0" Source="../Images/mammutLogoTop1280.jpg" Margin="0"/> 

     <Grid Grid.Row="1" Margin="15,5,15,0"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="1*"/> 
       <RowDefinition Height="1*"/> 
       <RowDefinition Height="1.5*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="765*"/> 
       <ColumnDefinition Width="62*"/> 
       <ColumnDefinition Width="47*"/> 
      </Grid.ColumnDefinitions> 
      <TextBox x:Name="InputTxt" Template="{StaticResource TextBoxBaseControlTemplate}" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" Height="30" FontSize="14" VerticalContentAlignment="Center" /> 
      <Button x:Name="InputBtn" Template="{StaticResource RoundCornerBtn}" Grid.Column="1" Grid.Row="0" Margin="5,0,0,0" Height="30" FontSize="14" Background="#FF006561" Foreground="White" Grid.ColumnSpan="2" Content="{Binding LanguageModel.InputBtn}"/> 
      <TextBox x:Name="OutputTxt" Template="{StaticResource TextBoxBaseControlTemplate}" Grid.Column="0" Grid.Row="1" Margin="0,0,5,0" Height="30" FontSize="14" VerticalContentAlignment="Center"/> 
      <Button x:Name="OutputBtn" Template="{StaticResource RoundCornerBtn}" Grid.Column="1" Grid.Row="1" Margin="5,0,0,0" Height="30" FontSize="14" Background="#FF006561" Foreground="White" Grid.ColumnSpan="2" Content="{Binding LanguageModel.OutputBtn}"/> 
      <Button x:Name="ConvertBtn" Template="{StaticResource RoundCornerBtn}" Grid.Row="2" Grid.ColumnSpan="3" Margin="0,7,0,6" FontSize="16" Background="#FF006561" Foreground="White" FontWeight="Bold" Content="{Binding LanguageModel.ConvertBtn}"/> 
     </Grid>    
</Grid> 

私はLanguageModel のインスタンスのプロパティにアクセスするには、私のバインディングを設定することで失敗したかどうか知りませんまたは、RaisePropertyChanged()メソッドが間違った方法で実装されています。 私はこのようなバインディングを設定しようとしましたPath= LanguageModel.Property前に。 と私はモデルのプロパティをObservable Listに入れて、それを繰り返すことを試みました。

また、私がプログラムをデバッグしようとすると、LangaugeModelプロパティの上にマウスを置くと、デバッガはStackOverFlow例外でシャットダウンします。 例外から私のプログラムはどこかでループに巻き込まれていると推測しましたが、どこで見つけることができませんでした。

私は、これは私の問題を理解するのに十分な詳細であると思います。(初ポスト)

+0

XAMLマークアップでローカライゼーションを行うプロジェクトが存在することに注意してください(例:https://wpflocalizeextension.codeplex.com/)。あなたは自分でそれをする必要はありません。 –

+0

XAMLはパブリッククラスのパブリックプロパティでのみ正しく動作することを覚えています。内部を取り除き、すべてを公開してみてください。 – Atlasmaybe

+0

ちょうど気付いてください:あなたのloalizationのためにresxファイルを使用することができます –

答えて

1

注XAMLマークアップ(例えばwpflocalizeextension.codeplex.com)にローカライズを行うプロジェクトが存在すること。あなたは自分でそれをする必要はありません。

文字列がRESXファイルの場所にあります。

  • フォールバック言語:filename.resx
  • ドイツ語:filename.de.resx
  • フランス語:filename.fr.resx
+0

私はこれを試してみます。 – CyberNiinja

+0

働いてくれてありがとう、助けを借りて: – CyberNiinja

+0

@Cyber​​Ninja:よろしいですか? –

関連する問題