我想向我的Xamarin表單中添加一個Picker,該表單顯示模板名稱列表,並允許用戶選擇一個。如何在Xamarin Picker中調試綁定問題
這裏是以下形式:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DefectReport"
x:Class="DefectReport.VehiclePage">
<ContentPage.Content>
<ScrollView>
<StackLayout>
<Label x:Name="Message" TextColor="Red" />
<Label Text="Registration Number" />
<Entry Text="{Binding Vehicle.RegistrationNumber}" />
<Label Text="Description" />
<Entry Text="{Binding Vehicle.Description}" />
<Entry Text="Vehicle Type" />
<Picker ItemsSource="{Binding Templates, PresentationTraceSources.TraceLevel=High}" ItemDisplayBinding="{Binding TemplateName}" SelectedItem="{Binding SelectedTemplate}"/>
<Button Text="Save" Clicked="SaveButton_Clicked" />
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
這裏是後面的代碼:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class VehiclePage : ContentPage {
Vehicle Vehicle { get; set; }
ObservableCollection<Template> Templates { get; set; }
Template SelectedTemplate { get; set; }
public VehiclePage(Vehicle vehicle, List<Template> templates) {
Vehicle = vehicle ?? new Vehicle();
Templates = new ObservableCollection<Template>(templates);
SelectedTemplate = templates.FirstOrDefault(t => t.ServerRecordId == Vehicle.TemplateId) ?? templates.FirstOrDefault();
BindingContext = this;
InitializeComponent();
}
private async void SaveButton_Clicked(object sender, EventArgs e) {
}
}
模板和車輛類別:
public class Template {
[PrimaryKey]
[AutoIncrement]
public int DeviceRecordId { get; set; }
[Indexed]
public int idTemplate { get; set; }
public string TemplateName { get; set; }
public string TemplateData { get; set; }
[Ignore]
public int ServerRecordId {
get {
return idTemplate;
}
set {
idTemplate = value;
}
}
}
public class Vehicle {
[PrimaryKey]
[AutoIncrement]
public int DeviceRecordId { get; set; }
[Indexed]
public int idVehicle { get; set; }
[Indexed]
public string RegistrationNumber { get; set; }
public string Description { get; set; }
public int TemplateId { get; set; }
[Ignore]
public ServerRecordId {
get {
return idVehicle;
}
set {
idVehicle = value;
}
}
}
當我顯示形式,供給列表的1個模板中,下拉列表中沒有模板。
另外,當我向表單輸入數據時,在SaveButton_Clicked方法中,即使輸入了一些數據,Vehicle.RegistrationNumber和Vehicle.Description也沒有填充。
我看不到如何調試 - 這一切都隱藏起來!
你只能綁定到屬性 - 模板不是屬性 – Jason
要調試綁定,通常:'ControlProperty =「{綁定MyVMProp,PresentationTraceSources.TraceLevel = High}'。在調試器的運行時,您會在VS Output窗格中看到大量的調試信息。 –
我現在已將可綁定項目更改爲屬性(我已更改問題中的代碼)。我添加了PresentationTraceSources.TraceLevel = High,但它停止窗體編譯「錯誤:值不能爲空。錯誤:參數名稱:clrNamespace「 –