DatePicker为Visibility引发异常
本文关键字:异常 Visibility DatePicker | 更新日期: 2023-09-27 18:28:16
我在自己的视图中添加了一个DatePicker,但Visibility属性(我尚未配置)出现错误。
错误为:
System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value '8/18/1993' (type 'System.String'); BindingExpression: Path='DateOfBirth' DataItem='Gui.ViewModels.RegisterPersonalViewModel' (HashCode=64515557); target element is 'Microsoft.Phone.Controls.DatePicker' (Name='DateOfBirth'); target property is 'Visibility' (type 'System.Windows.Visibility').. System.ArgumentException: Requested value '8/18/1993' was not found.
at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at MS.Internal.SilverlightTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, CultureInfo culture, Boolean isForward)
at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertToTarget(Object value).
视野看起来像:
<toolkit:DatePicker x:Name="DateOfBirth" />
视图模型中的属性:
public DateTime DateOfBirth { get; set; }
因此,我没有将Visibility属性映射到字段的任何内容。那么,它为什么要把它绑定起来呢?
我还尝试添加这样的可见性绑定:
<toolkit:DatePicker x:Name="DateOfBirth"
Visibility="{Binding Path=IsDateOfBirthVisible,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
并添加一个字段:
public DateTime DateOfBirth { get; set; }
public bool IsDateOfBirthVisible { get; set; }
但我得到了以下例外:
System.Windows.ni.dll中发生类型为"System.exception"的异常,在托管/本机边界之前未进行处理
我还尝试添加自定义约定:
ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueProperty, "Value", "SelectedDate");
ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueStringFormatProperty, "ValueStringFormat", "ValueStringFormatChanged");
ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueStringProperty, "ValueString", "ValueStringChanged");
ConventionManager.AddElementConvention<DatePicker>(UIElement.VisibilityProperty, "Visibility", "VisibilityChanged");
但这并没有什么区别。
我对caliburn和windows手机都是新手,所以可能在某个地方犯了一个愚蠢的错误。
我该如何摆脱这个例外?
(日期选择器来自https://phone.codeplex.com/)
Caliburn.Microo在FrameworkElement
上定义了一个"catch-all"约定,它将视图模型上的匹配属性绑定到FrameworkElement.VisibilityProperty
AddElementConvention<FrameworkElement>(
FrameworkElement.VisibilityProperty, "DataContext", "Loaded");
由于Caliburn.Micro不知道Microsoft.Phone.Controls.DatePicker
类型或其基类(DateTimePickerBase
、Control
),它会退回到FrameworkElement
约定,并尝试将您的DateTime
绑定到DatePicker
VisibilityProperty
,因此您可以在Output窗口中看到异常。
有两种解决方案:
- 手工装订:
<toolkit:DatePicker Value="{Bidning DateOfBirth}" />
创建一个自定义约定,在本例中为:
ConventionManager.AddElementConvention<DatePicker>( DateTimePickerBase.ValueProperty, "Value", "SelectedDate");
注意:对于给定的类型,只能有一个convetion,所以这应该是您的only
AddElementConvention<DatePicker>
调用。