获取系统文化和国家信息(CultureInfo)类:在组合框中选择一个国家,则会显示操作系统中该国家的有关信息。

题目要求如下

美化后如下

xaml代码

xaml代码主要是布局,具体实现为C#

<Windowx:Class="WpfApp1.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:local="clr-namespace:WpfApp1"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="演示获取系统文化和国家信息" Width="360" Height="260"MinWidth="340" MinHeight="240" MaxHeight="280" MaxWidth="400"WindowStartupLocation="CenterScreen"Loaded="Window_Loaded"mc:Ignorable="d"><Grid Margin="10"><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="*"/><ColumnDefinition Width="80"/><ColumnDefinition Width="4"/><ColumnDefinition Width="80"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="25"/><RowDefinition Height="4"/><RowDefinition Height="*"/><RowDefinition Height="4"/><RowDefinition Height="25"/></Grid.RowDefinitions><TextBlock Text="选择国家:" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"/> <Image Source="E:/C#/WpfApp1/WpfApp1/Resources/earth.png" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="3" Width="100" Height="100"/><ComboBox Name="countryComboBox" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4" Style="{DynamicResource ComboBoxStyle}" DisplayMemberPath="Country" SelectionChanged="Country_Changed"/><GroupBox Name="countryGroupBox" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="5" BorderBrush="Black"><StackPanel><TextBlock x:Name="Info1"/><TextBlock x:Name="Info2"/><TextBlock x:Name="Info3"/><TextBlock x:Name="Info4"/><TextBlock x:Name="Info5"/><TextBlock x:Name="Info6"/></StackPanel></GroupBox><Button x:Name = "button1" Content = "保存" Grid.Column="2" Grid.Row="4" Click = "OnClickSave" Style="{StaticResource MyWpfButton}"/><Button x:Name = "button2" Content = "添加" Grid.Column="4" Grid.Row="4" Click = "OnClickAdd" Style="{StaticResource MyWpfButton}"/></Grid>
</Window>

C#代码

C#代码,样式可以自行调整

using System;
using System.IO;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using System.ComponentModel;
using Microsoft.VisualBasic;namespace WpfApp1
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{private static List<CountryInfo> lstCountry; //数据源private BindingList<CountryInfo> bindingList; //用于更新comboBoxpublic MainWindow(){InitializeComponent();}private void Window_Loaded(object sender, RoutedEventArgs e) //初始化国家数据,加载ComboBox{lstCountry = new List<CountryInfo>();// 用于绑定数据源//try//{//    StreamReader sr = new StreamReader("E:/C#/WpfApp1/WpfApp1/country.txt"); // 创建一个 StreamReader 的实例来读取文件 //    {//        string line;//        while ((line = sr.ReadLine()) != null) // 从文件读取一行,直到文件的末尾 //        {//            //中国 CN zh-CN//            lstCountry.Add(new CountryInfo { Country = line.Split()[0], Region = line.Split()[1], Culture = line.Split()[2] });//        }//    }//}//catch (Exception exc)//{//    // 向用户显示出错消息//    Console.WriteLine("The file could not be read:");//    Console.WriteLine(exc.Message);//}lstCountry.Add(new CountryInfo { Country = "中国", Region = "CN", Culture = "zh-CN" });CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);for (int i = 1; i < cultures.Length; i++){if (cultures[i].Name.Split('-').Length == 1) //Length=1为语言名称{continue;}try{if (cultures[i].Name.Split('-').Length == 2){//中国 CN zh-CNRegionInfo region = new RegionInfo(cultures[i].Name);lstCountry.Add(new CountryInfo { Country = region.DisplayName, Region = cultures[i].Name.Split('-')[1], Culture = cultures[i].Name });}else if (cultures[i].Name.Split('-').Length == 3){RegionInfo region = new RegionInfo(cultures[i].Name);lstCountry.Add(new CountryInfo { Country = region.DisplayName, Region = cultures[i].Name, Culture = cultures[i].Name });}}catch (Exception exc){//向用户显示出错消息Console.WriteLine(exc.Message);continue;}}this.countryComboBox.ItemsSource = lstCountry;this.countryComboBox.SelectedIndex = 0; // 默认选中中国}private void Country_Changed(object sender, SelectionChangedEventArgs e){CountryInfo selectedCountry = (CountryInfo)countryComboBox.Items[countryComboBox.SelectedIndex];//当前选中加1//CountryInfo selectedCountry;//if (countryComboBox.SelectedIndex + 1 == countryComboBox.Items.Count)//{//    selectedCountry = (CountryInfo)countryComboBox.Items[0];//}//else//{//    selectedCountry = (CountryInfo)countryComboBox.Items[countryComboBox.SelectedIndex + 1];//}string country = selectedCountry.Country; //国家string region = selectedCountry.Region; //区域string culture = selectedCountry.Culture; //文化//CultureInfo类基于 RFC 4646 为每个区域性指定唯一名称 例如:中国 zh-CNCultureInfo myCultureInfo = new CultureInfo(culture);//与 CultureInfo 类不同, RegionInfo 类不表示用户首选项,且不依赖于用户的语言或区域性。//RegionInfo 是在 ISO 3166 中为国家 / 地区定义的由两个字母组成的代码之一 例如:中国CNRegionInfo myRegionInfo = new RegionInfo(region);this.countryGroupBox.Header = country + "国家和文化信息";this.Info1.Text = "国家全名:" + myRegionInfo.DisplayName;this.Info2.Text = "国家英文名:" + myRegionInfo.EnglishName;this.Info3.Text = "货币符号:" + myRegionInfo.CurrencySymbol;this.Info4.Text =  "是否使用公制:" + (myRegionInfo.IsMetric ? "是" : "否").ToString();this.Info5.Text = "三字地区码:" + myRegionInfo.ThreeLetterISORegionName;this.Info6.Text = "语言名称:" + myCultureInfo.DisplayName;}private void OnClickSave(object sender, RoutedEventArgs e){CountryInfo selectedCountry = (CountryInfo)countryComboBox.Items[countryComboBox.SelectedIndex];string country = selectedCountry.Country; //国家using (StreamWriter sw = new StreamWriter("E:/C#/WpfApp1/WpfApp1/Info.txt", true)) //以追加的方式写入文件{sw.WriteLine(country + "国家和文化信息");sw.WriteLine(this.Info1.Text);sw.WriteLine(this.Info2.Text);sw.WriteLine(this.Info3.Text);sw.WriteLine(this.Info4.Text);sw.WriteLine(this.Info5.Text);sw.WriteLine(this.Info6.Text);sw.WriteLine();}MessageBox.Show(country + "国家和文化信息保存成功");}private void OnClickAdd(object sender, RoutedEventArgs e){string str = Interaction.InputBox("请输入需要添加的国家信息,输入格式:中国 CN zh-CN", "添加国家", "", -1, -1);if (str.Split().Length != 3){MessageBox.Show("输入格式有误!");return;}else if (str.Split().Length == 3){try{Console.WriteLine(new RegionInfo(str.Split()[1]));Console.WriteLine(new CultureInfo(str.Split()[2]));}catch (Exception){MessageBox.Show("输入内容有误!");return;}}lstCountry.Insert(0, new CountryInfo { Country = str.Split()[0], Region = str.Split()[1], Culture = str.Split()[2] });bindingList = new BindingList<CountryInfo>(lstCountry);this.countryComboBox.ItemsSource = bindingList;MessageBox.Show("												

C# WPF 获取系统文化和国家信息(CultureInfo)类相关推荐

  1. C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表

    OpenFileDialog openFileDialog1 = new OpenFileDialog(); if(openFileDialog1.ShowDialog() == DialogResu ...

  2. 工具及方法 - Process Explorer以及类似工具,用来获取系统运行的进程信息

    下载Process explorer: Process Explorer - Sysinternals | Microsoft Learn Process explorer简介 有没有想过哪个程序打开 ...

  3. Api demo源码学习(8)--App/Activity/QuickContactsDemo --获取系统联系人信息

    本节通过Content Provider机制获取系统中的联系人信息,注意这个Anctivity直接继承的是ListActivity,所以不再需要setContentView函数来加载布局文件了(我自己 ...

  4. Linux应用开发4 如何获取系统参数信息(监测终端信息)

    通过这一节的学习,你可以在UI界面做监测终端信息,如CPU 时间等各类系统参数,狂拽酷炫吊炸天 目录 系统基本参数 时间.日期(GMT.UTC.时区) 获取进程时间 产生随机数 休眠(重要) 申请堆内 ...

  5. 安卓获取系统照片 (Kotlin)

    1.在activity里点击"获取照片",然后跳转到系统相册: fun open() { var intent: Intent = Intent("android.int ...

  6. 快速获取Windows系统上的国家和地区信息

    Windows系统上包含了200多个国家和地区的数据,有时候编程需要这些资料.以下代码可以帮助你快速获取这些信息. 将Console语句注释掉,可以更快的完成分析. 1 static void Mai ...

  7. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  8. IOS获取系统通讯录联系人信息

    2019独角兽企业重金招聘Python工程师标准>>> IOS获取系统通讯录联系人信息 一.权限注册 随着apple对用户隐私的越来越重视,IOS系统的权限设置也更加严格,在获取系统 ...

  9. R语言sys方法:sys.info函数获取系统和用户信息、sys.localeConv函数获取当前区域中的数字和货币表示的详细信息、sys.setFileTime函数更改文件的时间

    R语言sys方法:sys.info函数获取系统和用户信息.sys.localeConv函数获取当前区域中的数字和货币表示的详细信息.sys.setFileTime函数更改文件的时间 目录

最新文章

  1. 适配器模式理解和使用
  2. openlayers基础(一)——Map
  3. Docker 创建镜像
  4. VxWorks中Timer机制
  5. powerpc 汇编linux,PowerPc下的寻址模式
  6. java string能存储多长_String 有多长?
  7. 排除jar_通过IDEA快速定位和排除依赖冲突
  8. python爬虫新闻网页的浏览量转载量,Python爬取新闻网标题、日期、点击量
  9. Spring读书笔记(一)
  10. 2022 CVPR 三维人体重建相关论文汇总(3D Human Reconstruction)
  11. 在北极都可以穿短袖了,温度飙升至32.5℃
  12. 折叠面板(Collapse)
  13. 【官方文档】Fluent Bit 数据管道之过滤插件(Kubernetes)
  14. 【判断一个数是不是素数】
  15. DataFactory造数-前期准备工作(DF安装、myodbc32的安装与配置、Oracle客户端的安装与配置)
  16. 基于SVM的车牌识别
  17. onload和ready的不同
  18. 3D打印机喷头堵塞维修
  19. (转) 孝心是无价的
  20. week_03_常用类以及接口

热门文章

  1. Spring Boot基于注解方式处理接口数据脱敏
  2. java判断任意两数的最小公倍数和最大公约数
  3. 【动手学深度学习PyTorch版】12 卷积层
  4. 枚举的练习、声明一个枚举类型Status, Status(员工状态),可以限定为4个:空闲(Free),忙(Busy),休假(Vocation),离职(Left)
  5. python字典取值_python字典,python字典取值
  6. C语言百日刷题第八天
  7. 过滤器实现单一用户登录
  8. 用HBuilder开发的基于MUI和H5+的APP开发及上架经历
  9. CefInitialize崩溃 Cef白屏
  10. RHCA考试基础(三)