网站运营有前途吗去年做那个网站致富
news/
2025/9/23 8:55:47/
文章来源:
网站运营有前途吗,去年做那个网站致富,北京网页设计师培训多少钱,厦门网站建设的公司哪家好关于产生错误 “The as operator must be used with a reference type or nullable type (System.DateTime is a non-nullable value type) ” 今天写数据转换器#xff0c;需要将按照时间值显示不同的时间格式字符串。 结果在Convert里发现这么写报错。 public object Conve…关于产生错误 “The as operator must be used with a reference type or nullable type (System.DateTime is a non-nullable value type) ” 今天写数据转换器需要将按照时间值显示不同的时间格式字符串。 结果在Convert里发现这么写报错。 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime time value as DateTime; } 该问题是因为DateTime 为值类型不能用引用类型或空类型。我们可以按如下方法写这段代码。 //DateTime time value as DateTime; 这种写法就不对 因为DateTime为值类型 //DateTime time value as DateTime? ?? _NullTime; 这种写法虽然不报错用起来也没问题不过也不合适 DateTime time (DateTime)value; //这种是正确写法 多谢阿干童鞋指正 贴一下这个数据转换器 需求 绑定时间为当日的 只显示“时分” 绑定时间为当年且当日前的 显示 “月-日 时分” 绑定时间为往年的 显示 “年份 月-日 时分” 数据转换器 public class TimeFormatConverter:IValueConverter{private static DateTime _Now DateTime.Now;private static DateTime _UndefineTime new DateTime(0);public object Convert(object value, Type targetType, object parameter, CultureInfo culture){//DateTime time value as DateTime;//DateTime time value as DateTime? ?? _NullTime;DateTime time (DateTime)value;if (time _UndefineTime)return 绑定时间有误;int offYear _Now.Year - time.Year;int offDay _Now.Date.Subtract(time.Date).Days;if (offYear 1){return string.Format({0:yyyy-MM-dd HH:mm}, time);}else if (offDay 1){return string.Format({0:MM-dd HH:mm}, time);}else{return string.Format({0:HH:mm}, time);}}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}} xaml中声明集合 xmlns:Convclr-namespace:MyPageTabDemo.Utils.Converter 使用 TextBlock Text{Binding Timestamp, Converter{StaticResource TimeFmtConverter}} .../转载于:https://www.cnblogs.com/longlww/archive/2011/08/30/2159294.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/911965.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!