내 응용 프로그램에서 여러 언어로 작업하는 임.
내게 MVVM 패턴으로이 작업을 시도하고 있으므로 CodeProject 및 Tutorialspoint에서 자습서를 읽습니다.WPF 뷰 모델의 모델 속성에 바인딩 [MVVM]
Heres는 내 프로젝트 구조 :
Model
ConverterModel.cs
LanguageModel.cs
ViewModel
ConverterViewModel.cs
View
ConverterView.xaml
ConverterView.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
는 내가 필요로하는 모든 언어 문자열에 대한 모델을 만들어 :
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));
}
}
}
}
가 그럼 난에 그 모델의 인스턴스를 만들 내 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;
}
}
}
내 바인딩은 다음과 같이 설정합니다.
MainWindow를 :
<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() 메서드가 잘못 구현됩니다. 전에 Bindings를 Path= LanguageModel.Property
처럼 설정하려고했습니다. 그리고 Observable List에 모델 프라퍼티를 넣으려고 시도 했었고 반복도 했었습니다.
또한 디버그 프로그램을 시도하고 마우스를 LangaugeModel 속성 위로 가져 가면 디버거가 StackOverFlow 예외로 종료됩니다. 지금 예외에서 나는 내 프로그램이 어딘가에 루프에 잡히는 것을 짐작하지만, 나는 couldnt한다.
나는이 (예를 들어 wpflocalizeextension.codeplex.com) XAML 마크 업에 현지화 할 프로젝트가 존재하는 것이 내 문제. (먼저 글)
XAML 마크 업 (예 : https://wpflocalizeextension.codeplex.com/)에는 현지화 작업을 수행하는 프로젝트가 있습니다. 너 혼자 힘으로하지 않아도 돼. –
XAML은 공용 클래스의 public 속성과 만 올바르게 작동한다는 것을 기억합니다.내부를 제거하고 모두 공개로 설정해보십시오. – Atlasmaybe
주목해야 할 점은 당신의 loalization을 위해 resx 파일을 사용할 수 있다는 것입니다. –