简体   繁体   English

DateTime XAML WinRT

[英]DateTime XAML WinRT

Passing a string to the DependencyProperty of a control that takes a DateTime does not seem to be allowed: 似乎不允许将字符串传递给采用DateTime的控件的DependencyProperty

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. 我想解决方法是提供一个IValueConverter来将字符串转换为DateTime对象。 For Scheduler/Calender like controls this is a little annoying. 对于Scheduler / Calender这样的控件,这有点烦人。

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. TypeConverter在WinRT中不可用,尽管该平台似乎为许多UI类型提供了一些内置的转换-这种隐式转换不是其中之一。 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. 您可以确保您的DateTime属性绑定到DateTime视图模型属性。
  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. 将您的属性定义为String类型,并在控件内部进行转换-如果通常使用XAML字符串初始化该属性。 It would also be worth it to append 'String' to the name of the property to make it clear that it's a string - eg 'StartDateString'. 最好在属性名称后附加“字符串”,以明确表示它是字符串,例如“ 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. 如果您想同时使用DateTimeString类型的控件-您可以同时拥有这两种类型的属性并在内部进行同步,请确保防止在属性更改处理程序中重新输入。
  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. 将属性声明为Object类型,并检测要设置的值是直接设置DateTime值,从String转换还是其他类型( DateTimeOffsetTimeSpan ,...?)的类型,或者抛出不支持的值。

Unfortunately until the Windows platform teams add support for TypeConverter attribute - you don't have a pretty solution. 不幸的是,直到Windows平台团队添加对TypeConverter属性的支持-您还没有一个漂亮的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM