获取系统文化和国家信息(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. 「Excel技巧」Excel技巧之如何看文件里的宏?
  2. 《陶哲轩实分析》引理17.2.4证明_导数的唯一性
  3. Python---进阶---logging---logger
  4. C语言中的static 详细分析
  5. 无边框对话框改变大小
  6. php 验证qq密码错误,QQ输入正确密码却验证错误的解决办法
  7. FineReport——JS二次开发(局部刷新)
  8. framework dyld: Symbol not found: _OBJC_CLASS_xxx
  9. monaco-editor 监听保存按钮
  10. 【POJ3277】City Horizon,线段树
  11. php的冒泡排序的意思,冒泡排序是什么意思
  12. 与数据相关的运算符和伪指令
  13. 线性代数学习笔记——第六讲——矩阵的转置
  14. 蚂蚁金服实习三面,offer已拿。我总结了所有面试题,其实也不过如此!!
  15. 慧都科技:软件正版化不会一蹴而就 但趋势明显
  16. 残酷事实:程序员没有真正的「睡后收入」,解决办法是利用「复利思维」放大「复业收入」...
  17. URAL 1389 Roadworks 贪心
  18. 【数智化人物展】网智天元莫倩:“感、联、知、控”,四步方可打造企业数智化转型升级路径...
  19. cocos creator3.3.0休闲游戏(云浮消消乐)源码H5+安卓+IOS三端源码
  20. POI - Excel 打印配置

热门文章

  1. 电脑动态盘转换基本盘怎么操作?
  2. 怎么登录服务器上的网页,云服务器怎么在网页上登录
  3. 理解QPS、TPS、RT、吞吐量
  4. Raspuberry PI3 RPi.GPIO 官方文档翻译
  5. 存储基础知识之固态硬盘
  6. 亚信科技前端实习面试题
  7. Chrome浏览器不提示保存密码了怎么办?
  8. AutoCAD2021使用方法与小技巧总结1
  9. 单例模式,懒汉饿汉,线程安全,double checked locking的问题
  10. 无盘服务器多机启动慢,网卡PNP驱动兼容问题导致无盘客户机启动获取DHCP后白条时间长、滚动圈数多、黑屏时间...