我想将我的单语言项目改为多语言,所以我使用ResourceDictionary这样做:
XAML
<Button Content="{DynamicResource LanguageSetting}" Click="btn_LanguageSetting_Click"/>
代码背后
public static string windowCurrentLanguageFile = "Language/en.xaml";
private void btn_LanguageSetting_Click(object sender, RoutedEventArgs e)
{
windowCurrentLanguageFile = windowCurrentLanguageFile == "Language/en.xaml"
? "Language/ch.xaml"
: "Language/en.xaml";
var rd = new ResourceDictionary() { Source = new Uri(windowCurrentLanguageFile, UriKind.RelativeOrAbsolute) };
if (this.Resources.MergedDictionaries.Count == 0)
this.Resources.MergedDictionaries.Add(rd);
else
this.Resources.MergedDictionaries[0] = rd;
}
这对我来说很好。但我有ItemsControl
<ItemsControl ItemsSource="{Binding ItemOperate}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewmodel:SelectableViewModel}">
<Border x:Name="Border" Padding="0,8,0,8" BorderThickness="0 0 0 1" BorderBrush="{DynamicResource MaterialDesignDivider}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Checkerz" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton VerticalAlignment="Center" IsChecked="{Binding IsSelected}"
Style="{StaticResource MaterialDesignActionLightToggleButton}"
Content="{Binding Code}" />
<StackPanel Margin="8 0 0 0" Grid.Column="7">
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock Text="{Binding Description}" />
</StackPanel>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource MaterialDesignSelection}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
哪个绑定到 ViewModel ,如下所示:
public class SelectableViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected == value) return;
_isSelected = value;
OnPropertyChanged();
}
}
private char _code;
public char Code
{
get { return _code; }
set
{
if (_code == value) return;
_code = value;
OnPropertyChanged();
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged();
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
if (_description == value) return;
_description = value;
OnPropertyChanged();
}
}
}
并且
public MainViewModel()
{
_itemOperate = CreateData();
}
private static ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>
{
new SelectableViewModel
{
Code = 'E',
Name = "Erase",
Description = "Erase The MCU Chip By Page"
},
new SelectableViewModel
{
Code = 'D',
Name = "Detect",
Description = "Detect The MCU Flash",
},
new SelectableViewModel
{
Code = 'P',
Name = "Programming",
Description = "Programming The MCU Chip By Hex File",
},
new SelectableViewModel
{
Code = 'V',
Name = "Verify",
Description = "Verify The Downing Code",
},
new SelectableViewModel
{
Code ='L',
Name = "Lock",
Description = "Lock The Code To Protect The MCU",
}
};
}
那么我应该如何将其改为多种语言?提前谢谢!
答案 0 :(得分:2)
首先,我建议您更改本地化引擎。 有很多不同的方式。 有一个最简单的变体: https://www.codeproject.com/Articles/299436/WPF-Localization-for-Dummies
此外,此工具将帮助您管理资源文件: https://marketplace.visualstudio.com/items?itemName=TomEnglert.ResXManager
你问题的答案: 如果要本地化模型,则应使用资源字典创建它,而不是使用硬编码字符串。 如果你像上面提到的那样实现你的本地化引擎,这很容易 制品
{
return new ObservableCollection<SelectableViewModel>
{
new SelectableViewModel
{
Code = 'E',
Name = YourResourcesProject.Resources.Erase,
Description = YourResourcesProject.Resources.EraseTheMCUChipByPage
},
new SelectableViewModel
{
Code = 'D',
Name = YourResourcesProject.Resources.Detect,
Description = YourResourcesProject.Resources.DetectTheMCUFlash
},
new SelectableViewModel
{
Code = 'P',
Name = YourResourcesProject.Resources.Programming,
Description = YourResourcesProject.Resources.ProgrammingTheMCUChipByHexFile
},
new SelectableViewModel
{
Code = 'V',
Name = YourResourcesProject.Resources.Verify,
Description = YourResourcesProject.Resources.VerifyTheDowningCode
},
new SelectableViewModel
{
Code ='L',
Name = YourResourcesProject.Resources.Lock,
Description = YourResourcesProject.Resources.LockTheCodeToProtectTheMCU
}
};
}
答案 1 :(得分:0)
不要改变任何事情......将您的SelectableViewModel集合转换为XML并更改CreateData以将其加载到本地化版本中 - 您可以为每种语言提供文件或将所有语言混合在一起
答案 2 :(得分:0)
20行代码....如你所愿 - 不能比那更明确: - (
public class Selectable(View)Model
{
[XmlAttribute]
public string Code { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Description { get; set; }
}
///you can deserialize your view model directly
private ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>( Deserialize( file_name_code_lang.xml, SelectableViewModel) );
}
// or going through a model class
private ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>( Deserialize( file_name_code_lang.xml, SelectableModel ).Foreach(p=> new SelectableViewModel(p) );
}
static public object Deserialize(string filePath, Type objType)
{
object objToDeserialize = null;
XmlTextReader xmlReader = null;
XmlSerializer xmls = null;
try
{
xmlReader = new XmlTextReader(filePath);
xmls = new XmlSerializer(objType);
objToDeserialize = xmls.Deserialize(xmlReader);
}
catch (Exception err)
{
BusinessLogger.Manage(err);
return null;
}
finally
{
xmls = null;
xmlReader.Close();
}
return objToDeserialize;
}