'No property, BindableProperty, or event found for "SelectedDate"' 和 'The Property "SelectedDate" Was not found in type "DatePicker"'

'No property, BindableProperty, or event found for "SelectedDate"' and 'The Property "SelectedDate" Was not found in type "DatePicker"'

我正在努力将项目从标准 Xamarin.Forms 移动应用程序转换为使用 MVVM 格式。在转换为 MVVM 之前,应用程序的所有功能都按预期工作。在为我的一个视图设置我的 ViewModel 并将 SelectedDate 属性 添加到我的 DatePicker 后,我开始收到错误消息:The property 'SelectedDate' was not found in type 'DatePicker'.

开始清理所有并重建所有后,我也开始收到另一个错误:No property, BindableProperty, or event found for "SelectedDate", or mismatching type between value and property.

我的 XAML 文件的适用部分:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Class="StarTracker.AddAdventurePage"
             BackgroundColor="{StaticResource lightBlue}">
    <StackLayout HorizontalOptions="CenterAndExpand"
                 VerticalOptions="CenterAndExpand">
        <CollectionView x:Name="NewAdventure"
                        HorizontalOptions="CenterAndExpand"
                        SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Label Text="Date Played: "
                               FontFamily="Axion"
                               TextColor="{StaticResource red}"
                               Grid.Row="0"
                               Grid.Column="0"/>
                        <DatePicker x:Name="PlayedDatePicker"
                                    FontFamily="Axion"
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    SelectedDate="{Binding Date}"
                                    TextColor="{StaticResource darkBlue}"
                                    MinimumDate="08/17/2017"
                                    MaximumDate="{x:Static sys:DateTime.Today}">
                            <DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
                        </DatePicker>

视图模型:

        private DateTime date;
        public DateTime Date
        {
            get { return date; }
            set
            {
                date = value;
                OnPropertyChanged("Date");
            }
        }

找到 后,除了 INotifyPropertyChanged 之外,我还更改了我的 ViewModel 派生自 BindableObject,然后将日期 getter/setter 更改为:

public static readonly BindableProperty DateProperty = BindableProperty.Create(
            nameof(Date),
            typeof(DateTime),
            typeof(DatePicker));

        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set
            {
                SetValue(DateProperty, value);
                OnPropertyChanged("Date");
            }
        }

我仍然有 IntelliSense 告诉我 SelectedDate 不是 DatePicker 中的 属性,根据 https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datepicker?redirectedfrom=MSDN&view=netframework-4.8#properties,这是不准确的。 SelectedItem 绑定在我的其他 Pickers 中没有问题。

我缺少什么可以让我在 DatePicker 中使用 SelectedDate 属性 并通过我的 DataBindings 保存信息?

Xamarin Forms DatePicker 没有 SelectedDate 属性,它有 Date 属性

Date of type DateTime, the selected date, which defaults to the value DateTime.Today.

您在 post 中链接到的文档适用于 System.Windows.Controls.DatePicker