private void MainTextBox_TextChanged(object sender, TextChangedEventArgs e) { int newLen = MainTextBox.Text.Length; if (newLen >= lenBefore) { ScanCodeInfo info = AnsiiToScanCodeConverter.ScanCodeFromChar(MainTextBox.Text.Last()); if (info.ScanCode != 0) { pressedKeys[0] = info.SpecialKeys; pressedKeys[2] = info.ScanCode; (App.Current as App).MainController.SendKeyboadData(pressedKeys); pressedKeys[0] = 0; pressedKeys[2] = 0; (App.Current as App).MainController.SendKeyboadData(pressedKeys); } } lenBefore = newLen; }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": prop.SetValue(entity, DateTimeHelper.GetDateTimeFromXml(root.Element(propName).Value), null); break; case "Boolean": if (propName == "FuncFlag") { prop.SetValue(entity, root.Element(propName).Value == "1", null); } else { goto default; } break; case "Int32": prop.SetValue(entity, int.Parse(root.Element(propName).Value), null); break; case "Int64": prop.SetValue(entity, long.Parse(root.Element(propName).Value), null); break; case "Double": prop.SetValue(entity, double.Parse(root.Element(propName).Value), null); break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } } }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": case "Int32": case "Int64": case "Double": case "Nullable`1": //可为空对象 EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value); break; case "Boolean": if (propName == "FuncFlag") { EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1"); } else { goto default; } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; case "BatchJobInfo": //异步任务完成事件推送BatchJob BatchJobInfo batchJobInfo = new BatchJobInfo(); FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, batchJobInfo, null); break; case "AgentType": { AgentType tp; #if NET35 try { tp = (AgentType)Enum.Parse(typeof(AgentType), root.Element(propName).Value, true); prop.SetValue(entity, tp, null); } catch { } #else if (Enum.TryParse(root.Element(propName).Value, out tp)) { prop.SetValue(entity, tp, null); } #endif break; } case "Receiver": { Receiver receiver = new Receiver(); FillEntityWithXml(receiver, new XDocument(root.Element(propName))); prop.SetValue(entity, receiver, null); break; } default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } else if (prop.PropertyType.Name == "List`1")//客服回调特殊处理 { var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "RequestBase") { List <RequestBase> items = new List <RequestBase>(); foreach (var item in root.Elements("Item")) { RequestBase reqItem = null; var msgTypeEle = item.Element("MsgType"); if (msgTypeEle != null) { RequestMsgType type = RequestMsgType.DEFAULT; var parseSuccess = false; #if NET35 try { type = (RequestMsgType)Enum.Parse(typeof(RequestMsgType), msgTypeEle.Value, true); parseSuccess = true; } catch { } #else parseSuccess = Enum.TryParse(msgTypeEle.Value, true, out type); #endif if (parseSuccess) { switch (type) { case RequestMsgType.Event: { reqItem = new RequestEvent(); break; } case RequestMsgType.File: { reqItem = new RequestMessageFile(); break; } case RequestMsgType.Image: { reqItem = new Entities.Request.KF.RequestMessageImage(); break; } case RequestMsgType.Link: { reqItem = new Entities.Request.KF.RequestMessageLink(); break; } case RequestMsgType.Location: { reqItem = new Entities.Request.KF.RequestMessageLocation(); break; } case RequestMsgType.Text: { reqItem = new Entities.Request.KF.RequestMessageText(); break; } case RequestMsgType.Voice: { reqItem = new Entities.Request.KF.RequestMessageVoice(); break; } } } } if (reqItem != null) { FillEntityWithXml(reqItem, new XDocument(item)); items.Add(reqItem); } } prop.SetValue(entity, items, null); } } } }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; if (root == null) { return;//无法处理 } var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": case "Int32": case "Int64": case "Double": case "Nullable`1": //可为空对象 EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value); break; case "Boolean": if (propName == "FuncFlag") { EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1"); } else { goto default; } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 { var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "Account") { List <CustomerServiceAccount> accounts = new List <CustomerServiceAccount>(); foreach (var item in root.Elements(propName)) { var account = new CustomerServiceAccount(); FillEntityWithXml(account, new XDocument(item)); accounts.Add(account); } prop.SetValue(entity, accounts, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } else if (genericArguments[0].Name == "AroundBeacon") { List <AroundBeacon> aroundBeacons = new List <AroundBeacon>(); foreach (var item in root.Elements(propName).Elements("AroundBeacon")) { var aroundBeaconItem = new AroundBeacon(); FillEntityWithXml(aroundBeaconItem, new XDocument(item)); aroundBeacons.Add(aroundBeaconItem); } prop.SetValue(entity, aroundBeacons, null); } break; } case "Music": //ResponseMessageMusic适用 Music music = new Music(); FillEntityWithXml(music, new XDocument(root.Element(propName))); prop.SetValue(entity, music, null); break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; case "ChosenBeacon": //摇一摇事件通知 ChosenBeacon chosenBeacon = new ChosenBeacon(); FillEntityWithXml(chosenBeacon, new XDocument(root.Element(propName))); prop.SetValue(entity, chosenBeacon, null); break; case "AroundBeacon": //摇一摇事件通知 AroundBeacon aroundBeacon = new AroundBeacon(); FillEntityWithXml(aroundBeacon, new XDocument(root.Element(propName))); prop.SetValue(entity, aroundBeacon, null); break; default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } } }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": prop.SetValue(entity, DateTimeHelper.GetDateTimeFromXml(root.Element(propName).Value), null); break; case "Boolean": if (propName == "FuncFlag") { prop.SetValue(entity, root.Element(propName).Value == "1", null); } else { goto default; } break; case "Int32": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, int.Parse(root.Element(propName).Value), null); } break; case "Int64": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, long.Parse(root.Element(propName).Value), null); } break; case "Double": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, double.Parse(root.Element(propName).Value), null); } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } else if (genericArguments[0].Name.ToLower() == "irequestmessagebase") //群聊或者客服接口适用 { //文章下属节点item List <IRequestMessageBase> articles = new List <IRequestMessageBase>(); foreach (var item in root.Elements(propName)) { RequestMessageBase requestMessage; var msgType = MsgTypeHelper.GetRequestMsgType(item); switch (msgType) { case RequestMsgType.Text: requestMessage = new RequestMessageChatText(); break; case RequestMsgType.ShortVideo: requestMessage = new RequestMessageChatShortVideo(); break; case RequestMsgType.Image: requestMessage = new RequestMessageChatImage(); break; case RequestMsgType.Voice: requestMessage = new RequestMessageChatVoice(); break; case RequestMsgType.Event: var eventType = item.Element("Event").Value.ToLower(); switch (eventType) { case "create_chat": requestMessage = new RequestMessageChatEvent_Create(); break; case "update_chat": requestMessage = new RequestMessageChatEvent_Update(); break; case "quit_chat": requestMessage = new RequestMessageChatEvent_Quit(); break; default: throw new WeixinException(string.Format("EventType:{0} 在FillEntityWithXml中没有对应的处理程序!", eventType), new ArgumentOutOfRangeException()); //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } //requestMessage = new RequestMessageChatVoice(); break; default: requestMessage = new RequestMessageChatText(); break; } //var article = new Article(); FillEntityWithXml(requestMessage, new XDocument(item)); articles.Add(requestMessage); } prop.SetValue(entity, articles, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ChatInfo": //ResponseMessageVideo适用 ChatInfo chat = new ChatInfo(); FillEntityWithXml(chat, new XDocument(root.Element(propName))); prop.SetValue(entity, chat, null); break; case "Receiver": //ResponseMessageVideo适用 Receiver Receiver = new Receiver(); FillEntityWithXml(Receiver, new XDocument(root.Element(propName))); prop.SetValue(entity, Receiver, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; case "BatchJobInfo": //异步任务完成事件推送BatchJob BatchJobInfo batchJobInfo = new BatchJobInfo(); FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, batchJobInfo, null); break; default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } } }