Loading... # 一.事件监听 对TextBox的KeyUp事件进行监听处理。 ```c# <!--监听按键事件--> <i:Interaction.Triggers > <i:EventTrigger EventName="KeyUp" > <prism:InvokeCommandAction Command="{Binding TextChanged}" CommandParameter="5" /> </i:EventTrigger> </i:Interaction.Triggers> ``` 最初使用的 `TextChanged`Event,结果发现赋值会导致反复触发该事件,因此改用了`KeyUp`Event。 # 二.编码转换 ## 1.GB2312编码支持 使用nuget安装`system.text.encoding.codepages`,在使用GB2312编码之前,增加如下代码: ```c# Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); ``` 编码转换代码如下: ```c# private string _Text; private string _ANSI; private string _GBK; private string _Unicode; private string _Utf_8; private string _Utf_7; //文本 public string Text { get { return _Text; } set { SetProperty(ref _Text, value); } } //ASCII 编码 public string ANSI { get { return _ANSI; } set { SetProperty(ref _ANSI, value); } } //GB2312 编码 public string GBK { get { return _GBK; } set { SetProperty(ref _GBK, value); } } //Unicode 编码 public string Unicode { get { return _Unicode; } set { SetProperty(ref _Unicode, value); } } //Utf-8 编码 public string Utf_8 { get { return _Utf_8; } set { SetProperty(ref _Utf_8, value); } } //Utf-7 编码 public string Utf_7 { get { return _Utf_7; } set { SetProperty(ref _Utf_7, value); } } private DelegateCommand<string> _textChanged; public DelegateCommand<string> TextChanged => _textChanged ?? (_textChanged = new DelegateCommand<string>(_TextChanged)); void _TextChanged(string str) { try { switch (str) { case "0": //输入文本 if (Text?.Length > 0) { ANSI = BitConverter.ToString(Encoding.ASCII.GetBytes(Text)).Replace("-", " "); GBK = BitConverter.ToString(Encoding.GetEncoding("GB2312").GetBytes(Text)).Replace("-", " "); Unicode = BitConverter.ToString(Encoding.Unicode.GetBytes(Text)).Replace("-", " "); Utf_8 = BitConverter.ToString(Encoding.UTF8.GetBytes(Text)).Replace("-", " "); Utf_7 = BitConverter.ToString(Encoding.UTF7.GetBytes(Text)).Replace("-", " "); } else { ANSI = ""; GBK = ""; Unicode = ""; Utf_8 = ""; Utf_7 = ""; } break; case "1": // 输入ASCII编码 if(ANSI?.Length>0) { Text = Encoding.ASCII.GetString(ToBytesFromHexString(ANSI.Replace(" ",""))); GBK = BitConverter.ToString(Encoding.GetEncoding("GB2312").GetBytes(Text)).Replace("-", " "); Unicode = BitConverter.ToString(Encoding.Unicode.GetBytes(Text)).Replace("-", " "); Utf_8 = BitConverter.ToString(Encoding.UTF8.GetBytes(Text)).Replace("-", " "); Utf_7 = BitConverter.ToString(Encoding.UTF7.GetBytes(Text)).Replace("-", " "); } else { Text = ""; GBK = ""; Unicode = ""; Utf_8 = ""; Utf_7 = ""; } break; case "2": // 输入GB2312编码 if (GBK?.Length > 0) { Text = Encoding.GetEncoding("GB2312").GetString(ToBytesFromHexString(GBK.Replace(" ", ""))); ANSI = BitConverter.ToString(Encoding.ASCII.GetBytes(Text)).Replace("-", " "); Unicode = BitConverter.ToString(Encoding.Unicode.GetBytes(Text)).Replace("-", " "); Utf_8 = BitConverter.ToString(Encoding.UTF8.GetBytes(Text)).Replace("-", " "); Utf_7 = BitConverter.ToString(Encoding.UTF7.GetBytes(Text)).Replace("-", " "); } else { Text = ""; ANSI = ""; Unicode = ""; Utf_8 = ""; Utf_7 = ""; } break; case "3": // 输入Unicode编码 if (Unicode?.Length > 0) { Text = Encoding.Unicode.GetString(ToBytesFromHexString(Unicode.Replace(" ", ""))); ANSI = BitConverter.ToString(Encoding.ASCII.GetBytes(Text)).Replace("-", " "); GBK = BitConverter.ToString(Encoding.GetEncoding("GB2312").GetBytes(Text)).Replace("-", " "); Utf_8 = BitConverter.ToString(Encoding.UTF8.GetBytes(Text)).Replace("-", " "); Utf_7 = BitConverter.ToString(Encoding.UTF7.GetBytes(Text)).Replace("-", " "); } else { Text = ""; ANSI = ""; GBK = ""; Utf_8 = ""; Utf_7 = ""; } break; case "4": // 输入Utf-8编码 if (Utf_8?.Length > 0) { Text = Encoding.UTF8.GetString(ToBytesFromHexString(Utf_8.Replace(" ", ""))); ANSI = BitConverter.ToString(Encoding.ASCII.GetBytes(Text)).Replace("-", " "); GBK = BitConverter.ToString(Encoding.GetEncoding("GB2312").GetBytes(Text)).Replace("-", " "); Unicode = BitConverter.ToString(Encoding.Unicode.GetBytes(Text)).Replace("-", " "); Utf_7 = BitConverter.ToString(Encoding.UTF7.GetBytes(Text)).Replace("-", " "); } else { Text = ""; ANSI = ""; GBK = ""; Unicode = ""; Utf_7 = ""; } break; case "5": // 输入Utf-7编码 if (Utf_7?.Length > 0) { Text = Encoding.UTF7.GetString(ToBytesFromHexString(Utf_8.Replace(" ", ""))); ANSI = BitConverter.ToString(Encoding.ASCII.GetBytes(Text)).Replace("-", " "); GBK = BitConverter.ToString(Encoding.GetEncoding("GB2312").GetBytes(Text)).Replace("-", " "); Unicode = BitConverter.ToString(Encoding.Unicode.GetBytes(Text)).Replace("-", " "); Utf_8 = BitConverter.ToString(Encoding.UTF8.GetBytes(Text)).Replace("-", " "); } else { Text = ""; ANSI = ""; GBK = ""; Unicode = ""; Utf_8 = ""; } break; default: break; } } catch (Exception ex) { } } ``` # 三.效果  最后修改:2022 年 04 月 24 日 10 : 01 PM © 允许规范转载