更新Property时,需要确保在类构造函数中调用InitializeComponent()之前更新它,否则一旦表单加载,该值将重置为默认值。
例如活动内容页.cs
public partial class EventPage : ContentPage
{
public EventPage() // All pages need a default, empty constructor for the XAML previewer
{
EventDate = DateTime.Now; // Any default page values must be set before InitializeComponent() is called, otherwise the values get overridden
InitializeComponent();
BindingContext = this;
}
public DateTime EventDate { get; set; } // The properties that the page binds to need to have the same name as the properties in the model
}
和事件ContentPage.xaml
<VerticalStackLayout>
<DatePicker
Grid.Column="1"
Date="{Binding EventDate}"
Format="D"/>
</VerticalStackLayout>