Xamarin.forms Xamarin表单日期选择器未与UWP中的viewmodel属性绑定?

Xamarin.forms Xamarin表单日期选择器未与UWP中的viewmodel属性绑定?,xamarin.forms,xamarin.uwp,Xamarin.forms,Xamarin.uwp,我的DatePicker在UWP中不与我的viewmodel属性绑定,而在android和ios中工作 我的看法是: <DatePicker x:Name="StartDate" Date="{Binding StartDate, Mode=TwoWay}" Opacity="0.45" HeightRequest="50" Focused="OnScreenTapped" /> 我用StartDate=newdatetime(2000,01,01)初始化日期但它只显示当前日期。。

我的DatePicker在UWP中不与我的viewmodel属性绑定,而在android和ios中工作

我的看法是:

<DatePicker x:Name="StartDate" Date="{Binding StartDate, Mode=TwoWay}" Opacity="0.45" HeightRequest="50" Focused="OnScreenTapped" />
我用
StartDate=newdatetime(2000,01,01)初始化日期但它只显示当前日期。。。。。。
此外,当我在日期选择器中更改日期时,它在viewmodel中不会更改


非常感谢您的帮助…谢谢

我试图重现您的问题,但是
日期选择器
绑定在我这边起作用。您可以参考以下代码检查是否遗漏了一些关键步骤

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinDatePickerTest"
             x:Class="XamarinDatePickerTest.MainPage"
             xmlns:ViewMode ="clr-namespace:XamarinDatePickerTest.ViewModel"
             xmlns:sys="clr-namespace:System;assembly=System.Runtime"
             >
   <ContentPage.BindingContext>
        <ViewMode:MainPageViewModel/>
    </ContentPage.BindingContext>
    <StackLayout>
        <DatePicker VerticalOptions="CenterAndExpand" Date="{Binding Datetime}">
            <DatePicker.Format>yyyy-MM-dd</DatePicker.Format>
            <DatePicker.MinimumDate>
                <sys:DateTime x:FactoryMethod="Parse">
                    <x:Arguments>
                        <x:String>Jan 1 2000</x:String>
                    </x:Arguments>
                </sys:DateTime>
            </DatePicker.MinimumDate>
        </DatePicker>
    </StackLayout>
</ContentPage>


我已经上传到github,请检查。

我已经尝试复制您的问题,但是
DatePicker
绑定在我这边起作用。您可以参考以下代码检查是否遗漏了一些关键步骤

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinDatePickerTest"
             x:Class="XamarinDatePickerTest.MainPage"
             xmlns:ViewMode ="clr-namespace:XamarinDatePickerTest.ViewModel"
             xmlns:sys="clr-namespace:System;assembly=System.Runtime"
             >
   <ContentPage.BindingContext>
        <ViewMode:MainPageViewModel/>
    </ContentPage.BindingContext>
    <StackLayout>
        <DatePicker VerticalOptions="CenterAndExpand" Date="{Binding Datetime}">
            <DatePicker.Format>yyyy-MM-dd</DatePicker.Format>
            <DatePicker.MinimumDate>
                <sys:DateTime x:FactoryMethod="Parse">
                    <x:Arguments>
                        <x:String>Jan 1 2000</x:String>
                    </x:Arguments>
                </sys:DateTime>
            </DatePicker.MinimumDate>
        </DatePicker>
    </StackLayout>
</ContentPage>


我已经上传到github,请检查。

刚刚遇到同样的问题。我的问题是可取消的日期。默认情况下,DatePicker不允许为空的日期(默认为todays date),但您可以添加一个自定义控件使其工作。

刚刚遇到了相同的问题。我的问题是可取消的日期。默认情况下,DatePicker不允许为null的日期(默认为todays date),但您可以添加一个自定义控件以使其正常工作。

我认为,问题在于视图模型上的nullable属性。可能会有帮助。我认为,问题在于视图模型上的nullable属性。可能会有帮助。
public class MainPageViewModel : INotifyPropertyChanged
{
    private DateTime? _datetime;

    public DateTime? Datetime
    {
        get => _datetime;

        set
        {
            _datetime = value;
            OnPropertyChanged();
        }
    }
    public MainPageViewModel()
    {
        this.Datetime = new DateTime(2011, 12, 18);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}