Im在我的應用程序中使用多種語言。
我試圖用MVVM模式來做到這一點,這對我來說是新的,所以我從CodeProject和Tutorialspoint上閱讀了教程。在ViewModel中模型屬性的WPF綁定[MVVM]
我的繼承人項目結構:
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;
}
}
}
我有我的綁定設置是這樣的。
主窗口:
<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>
我不知道我是否未能在設定我的綁定來訪問的語言模型 實例的屬性或者RaisePropertyChanged()方法以錯誤的方式實現。 我試過設置我的綁定像這樣Path= LanguageModel.Property
之前。 我試圖把模型屬性放在可觀察列表中,並對其進行迭代,這也沒有奏效。
此外,當我嘗試調試程序並將鼠標懸停在LangaugeModel屬性上時,調試器將關閉一個StackOverFlow異常。 現在從異常我猜我的程序被圈入某個地方,但我找不到在哪裏。
我希望這是足夠詳細,這的確存在許多本地化的XAML標記(例如wpflocalizeextension.codeplex.com)項目,以瞭解我的問題。(第一篇文章)
請注意,在XAML標記中存在執行本地化的項目(例如https://wpflocalizeextension.codeplex.com/)。你不必自己做。 –
我記得XAML只能在公共類中使用公共屬性才能正常工作。嘗試刪除內部並公開設置。 – Atlasmaybe
只是要注意:你可以使用resx文件進行惡化 –