以下cs代码可以正常工作。
public partial class StudentDetailPage : ContentPage {
public StudentDetailViewModel VM { get; set; }
public StudentDetailPage(Model.Student stu)
{
InitializeComponent();
this.BindingContext = this.VM = new StudentDetailViewModel(stu);
this.Title = "Detail:"+ stu.Id;
}
}
但是我把它放到xaml中,它无法工作,所以如何使用self属性在xaml中编写它
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AppVersionUpdate.View"
x:Class="AppVersionUpdate.View.StudentDetailPage"
BindingContext="{Binding VM}"
>
<ContentPage.Content>
<Grid BindingContext="{Binding Item}" VerticalOptions="Start">
// some row and colomn define
<Label Text="Id:"/>
<Label Text="{Binding Id}" Grid.Column="1"/>
<Label Text="Name:" Grid.Row="1"/>
<Label Text="{Binding Name}" Grid.Row="1" Grid.Column="1"/>
<Label Text="Score:" Grid.Row="2"/>
<Label Text="{Binding Score}" Grid.Row="2" Grid.Column="1"/>
</Grid>
</ContentPage.Content>