/// <summary> /// 格式化输出 /// </summary> /// <param name="value"></param> /// <param name="clearErrorInfo">是否清空错误信息</param> /// <returns></returns> public override string Format(object value, bool clearErrorInfo = true) { try { if (clearErrorInfo) //清空错误信息 { ErrorInfos.Clear(); } var str = value.ToSpecObject <string> (); if (!str.IsValid(false)) { return(string.Empty); } var orgStr = str; if (DateTime.TryParse(str, out DateTime dt)) { str = dt.ToString(FormatString); } else { str = string.Empty; } if (!orgStr.Equals(str)) { ErrorInfos.Add($"日期时间格式应为 {GetReadableText(FormatString)}"); } return(str); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// 格式化输出 /// </summary> /// <param name="value"></param> /// <param name="clearErrorInfo">是否清空错误信息</param> /// <returns></returns> public override string Format(object value, bool clearErrorInfo = true) { try { if (clearErrorInfo) //清空错误信息 { ErrorInfos.Clear(); } var str = value?.ToString(); if (!str.IsValid(false)) { return(string.Empty); } var number = Convert.ToDecimal(str); var orgNumber = number; var minValue = GetFormatString(FormatMinValue)?.ToDecimal(); if (minValue != null) { number = Math.Max(number, minValue.Value); } var maxValue = GetFormatString(FormatMaxValue)?.ToDecimal(); if (maxValue != null) { number = Math.Min(number, maxValue.Value); } if (number != orgNumber) { if (minValue != null && maxValue != null) { ErrorInfos.Add($"只能输入 {minValue.Value} ~ {maxValue.Value} 之间的值"); } else if (minValue != null) { ErrorInfos.Add($"只能输入大于或等于 {minValue.Value} 的值"); } else if (maxValue != null) { ErrorInfos.Add($"只能输入小于或等于 {maxValue.Value} 的值"); } return(number.ToString()); } return(base.Format(value, false)); } catch (FormatException fe) { ErrorInfos.Add("格式不正确"); return(fe.Message); } catch (Exception ex) { ErrorInfos.Add(ex.Message); return(ex.Message); } }
private UIDInfo GetErrorInfo(int uid) { var info = ErrorInfos.FirstOrDefault(i => i.UID == uid); if (info == null) { info = new UIDInfo(uid); ErrorInfos.Add(info); } return(info); }
/// <summary> /// 格式化输出 /// </summary> /// <param name="value"></param> /// <param name="clearErrorInfo">是否清空错误信息</param> /// <returns></returns> public override string Format(object value, bool clearErrorInfo = true) { try { if (clearErrorInfo) //清空错误信息 { ErrorInfos.Clear(); } var str = value?.ToString(); if (!str.IsValid(false)) { return(string.Empty); } var number = Convert.ChangeType(str, NumberType); var fmt = GetFormatString(FormatFormat); if (!fmt.IsValid()) { return(base.Format(value, false)); } var data = number.Invoke("ToString", new[] { fmt }, new Type[] { typeof(string) }); if (!number.Equals(data)) { ErrorInfos.Add($"最多只能输入 {GetReadableText(fmt)} 位小数"); } return(base.Format(data, false)); } catch (FormatException fe) { ErrorInfos.Add("格式不正确"); return(fe.Message); } catch (Exception ex) { ErrorInfos.Add(ex.Message); return(ex.Message); } }
async Task GetErrorInfosAsync(int machineId) { if (IsBusy) { return; } try { IsBusy = true; var json = await Client.GetStringAsync(string.Format(Host + SelectPayment, machineId)); var errorInfos = ErrorInfo.FromJson(json); //Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, //() => // { // ErrorInfos.Clear(); // foreach (var error in errorInfos) // ErrorInfos.Add(error); // } //); ErrorInfos.Clear(); foreach (var error in errorInfos) { ErrorInfos.Add(error); } } catch (Exception ex) { Debug.WriteLine($"Unable to get Errors: {ex.Message}"); await Application.Current.MainPage.DisplayAlert("Error!", ex.Message, "OK"); } finally { IsBusy = false; } }