DateTime XAML WinRT [英] DateTime XAML WinRT

查看:165
本文介绍了DateTime XAML WinRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个字符串传递给需要 DateTime 的控件的 DependencyProperty 似乎不允许: / p>

无法将文本值'00:00:00'分配给DateTime类型的属性StartTime p>

只是我还是不应该这样做?我想的解决方法是提供一个IValueConverter将字符串转换为DateTime对象。对于安排程序/压缩程序,如控件,这有点烦人。



发现一些光?

解决方案

TypeConverter 在WinRT中不可用,而平台似乎有许多UI类型的内置转换 - 这种隐式转换不是其中之一。你有几个选择。


  1. 如上所述 - 你可以使用值转换器

  2. 您可以确保您的 DateTime 属性绑定到 DateTime 查看模型属性。

  3. 将属性定义为 String ,并在控件中进行转换 - 如果您通常使用XAML字符串初始化该属性。它也值得添加String到属性的名称,以表明它是一个字符串 - 例如。 'StartDateString'。

  4. 如果您希望使用 DateTime String types - 您可以拥有两种类型的属性,并在内部同步它们,确保防止在属性更改处理程序中重入。

  5. 将属性声明为类型对象并检测要设置的值的类型是直接设置 DateTime 值,从 String 或其他类型( DateTimeOffset TimeSpan ,...?)或抛出不受支持的值。 / li>

不幸的是,直到Windows平台团队添加对 TypeConverter 属性的支持 - 有一个漂亮的解决方案。


Passing a string to the DependencyProperty of a control that takes a DateTime does not seem to be allowed:

Cannot assign text value '00:00:00' into property 'StartTime' of type 'DateTime'

Is it just me or shouldn't this be possible? The workaround I suppose is to provide a IValueConverter to convert strings to DateTime objects. For Scheduler/Calender like controls this is a little annoying.

Shed some light?

解决方案

TypeConverter isn't available in WinRT and while the platform seems to have some built in conversions for many UI types - this implicit conversion is not one of those. You have a few options though.

  1. As you mentioned - you can use a value converter
  2. You can make sure your DateTime property is bound to a DateTime view model property.
  3. Define your property as type String and do the conversions inside of your control - if you would typically initialize that property with a XAML string. It would also be worth it to append 'String' to the name of the property to make it clear that it's a string - e.g. 'StartDateString'.
  4. If you would like to use your control both with DateTime and String types - you could have properties of both types and synchronize them internally, making sure to prevent reentrancy in property change handlers.
  5. Declare the property as type Object and detect what type the values being set are to either set a DateTime value directly, convert from String or other types (DateTimeOffset, TimeSpan, ...?) or throw for unsupported values.

Unfortunately until the Windows platform teams add support for TypeConverter attribute - you don't have a pretty solution.

这篇关于DateTime XAML WinRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆