gpt4 book ai didi

c# - DatePicker 抛出可见性异常

转载 作者:行者123 更新时间:2023-11-30 17:03:44 26 4
gpt4 key购买 nike

我在自己的 View 中添加了一个 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).

View 字段如下所示:

<toolkit:DatePicker x:Name="DateOfBirth" />

以及 View 模型中的属性:

public DateTime DateOfBirth { get; set; }

因此,我没有将 Visibility 属性映射到该字段的任何内容。那么它为什么要进行绑定(bind)呢?

我还尝试添加这样的可见性绑定(bind):

<toolkit:DatePicker x:Name="DateOfBirth"
Visibility="{Binding Path=IsDateOfBirthVisible,
Converter={StaticResource BooleanToVisibilityConverter}}"/>

并添加一个字段:

public DateTime DateOfBirth { get; set; }
public bool IsDateOfBirthVisible { get; set; }

但随后我得到以下异常:

An exception of type 'System.Exception' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

我还尝试添加自定义约定:

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 Phone 的新手,所以某处可能有一个愚蠢的错误。

如何摆脱该异常?

(日期选择器来自 https://phone.codeplex.com/)

最佳答案

Caliburn.Micro 在 FrameworkElement 上定义了一个“包罗万象”的惯例它将您在 View 模型上的匹配属性绑定(bind)到 FrameworkElement.VisibilityProperty

AddElementConvention<FrameworkElement>(
FrameworkElement.VisibilityProperty, "DataContext", "Loaded");

因为 Caliburn.Micro 不知道 Microsoft.Phone.Controls.DatePicker类型或其基类( DateTimePickerBaseControl )它回退到 FrameworkElement约定,它会尝试绑定(bind)您的 DateTimeDatePicker VisibilityProperty所以您会在“输出”窗口中看到异常。

有两种解决方法:

  • 手写绑定(bind):<toolkit:DatePicker Value="{Bidning DateOfBirth}" />
  • 创建自定义约定,在本例中为:

    ConventionManager.AddElementConvention<DatePicker>(
    DateTimePickerBase.ValueProperty, "Value", "SelectedDate");

    注意:给定类型只能有一个约定,因此这应该是您的唯一 AddElementConvention<DatePicker>打电话。

关于c# - DatePicker 抛出可见性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com