我已经在我的视图中添加了一个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");
但这没有什么区别。
我是校准和Windows手机的新手,所以在某处可能会有一个愚蠢的错误。
如何摆脱该异常?
(日期选择器来自https://phone.codeplex.com/)
答案 0 :(得分:2)
Caliburn.Micro在FrameworkElement
上定义了一个“catch all”约定,它将viewmodel上的匹配属性绑定到FrameworkElement.VisibilityProperty
AddElementConvention<FrameworkElement>(
FrameworkElement.VisibilityProperty, "DataContext", "Loaded");
因为Caliburn.Micro不知道Microsoft.Phone.Controls.DatePicker
类型或其基类(DateTimePickerBase
,Control
),所以它回退到FrameworkElement
约定并尝试绑定您的DateTime
到DatePicker
VisibilityProperty
,因此您会在“输出”窗口中看到异常。
有两种解决方案:
<toolkit:DatePicker Value="{Bidning DateOfBirth}" />
创建自定义约定,在这种情况下:
ConventionManager.AddElementConvention<DatePicker>(
DateTimePickerBase.ValueProperty, "Value", "SelectedDate");
注意:对于给定类型,您只能有一个对话,因此这应该是仅 AddElementConvention<DatePicker>
来电。