Visual Studio C#开发俄语词汇训练器:俄文显示为问号问题求助
解决方案:俄文显示问号与英文-俄文行对应显示
一、解决俄文显示为问号的问题
根据你的运行环境(控制台/UI应用),对应处理:
1. 控制台应用
- 设置控制台编码:在程序入口(
Main方法)开头添加以下代码,强制控制台使用UTF-8编码输出:
Console.OutputEncoding = System.Text.Encoding.UTF8; Console.InputEncoding = System.Text.Encoding.UTF8;
- 确保代码文件编码正确:在Visual Studio中,点击菜单「文件」→「高级保存选项」,将.cs文件的编码设置为
UTF-8,避免俄文字符在编译时被损坏。
2. WinForms/WPF UI应用
- 选择支持西里尔字母的字体:给显示俄文的控件(如Label、TextBox)设置字体为
Arial Unicode MS、Segoe UI或Times New Roman,这些字体包含完整的俄语字符集。 - 直接赋值俄文文本:.NET默认使用Unicode存储字符串,只要代码文件编码正确,直接给控件的
Text属性赋值俄文即可,无需额外转码。
二、实现英文文本下方显示对应俄文行
1. 控制台应用场景
直接按「英文行→俄文行」的顺序输出即可,示例代码:
// 定义词汇对集合 var vocabulary = new List<(string English, string Russian)> { ("Apple", "Яблоко"), ("Banana", "Банан"), ("Cat", "Кот") }; foreach (var item in vocabulary) { Console.WriteLine(item.English); Console.WriteLine(item.Russian); Console.WriteLine(); // 空行分隔每组词汇 }
2. WinForms UI场景
使用TableLayoutPanel控件,每行添加两个Label分别显示英文和俄文:
// 假设已有TableLayoutPanel控件tableLayoutPanel1 tableLayoutPanel1.ColumnCount = 1; tableLayoutPanel1.RowCount = vocabulary.Count; tableLayoutPanel1.AutoSize = true; for (int i = 0; i < vocabulary.Count; i++) { // 英文Label var engLabel = new Label { Text = vocabulary[i].English, Font = new Font("Segoe UI", 12), AutoSize = true }; // 俄文Label var rusLabel = new Label { Text = vocabulary[i].Russian, Font = new Font("Segoe UI", 12), AutoSize = true, ForeColor = Color.DarkGray // 可选:区分俄文颜色 }; tableLayoutPanel1.Controls.Add(engLabel, 0, i); tableLayoutPanel1.Controls.Add(rusLabel, 0, i); // 调整俄文Label位置到英文下方 rusLabel.Location = new Point(engLabel.Location.X, engLabel.Location.Y + engLabel.Height + 5); }
3. WPF UI场景
用StackPanel作为每行容器,包含两个TextBlock分别显示英文和俄文:
<!-- XAML示例 --> <ListBox ItemsSource="{Binding VocabularyList}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="5"> <TextBlock Text="{Binding English}" FontSize="14"/> <TextBlock Text="{Binding Russian}" FontSize="14" Foreground="Gray" Margin="0,2,0,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
对应的C# ViewModel代码:
public class VocabularyItem { public string English { get; set; } public string Russian { get; set; } } public class MainViewModel { public List<VocabularyItem> VocabularyList { get; set; } = new List<VocabularyItem> { new VocabularyItem { English = "Apple", Russian = "Яблоко" }, new VocabularyItem { English = "Banana", Russian = "Банан" } }; }
内容的提问来源于stack exchange,提问作者Rhys
相关产品推荐
相关产品推荐

