我已經嘗試了我能想到的所有東西以及我在網上找到的所有東西,但沒有任何運氣,仍然是一個空白頁面。
這是我的 xaml 檔案;
<ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" SelectedItem="{Binding SelectedItem}" SeparatorVisibility="None" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="55"/>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="36"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Year}" HorizontalTextAlignment="Center"/>
<Label Grid.Column="2" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Name}" HorizontalTextAlignment="Start" Grid.ColumnSpan="2"/>
<Switch x:Name="{Binding Id}" IsToggled="{Binding IsChecked, Mode=TwoWay}" Toggled="Handle_Toggled" Grid.Column="6" Grid.Row="0" Grid.ColumnSpan="2"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
我的視圖模型中的代碼看起來像這樣,我已經多次更改它,但這是現在的位置。
public ObservableCollection<AutoWithSwitch> MyList = new ObservableCollection<AutoWithSwitch>();
public ObservableCollection<AutoWithSwitch> AutoData = new ObservableCollection<AutoWithSwitch>(); //{ get { return MyList; } }
private IPopupNavigation _popup { get; set; }
private PopupPage _modalPage;
public event PropertyChangedEventHandler PropertyChanged;
public UpdateCarsViewModel()
{
//Analytics.TrackEvent("Top of Constructor in UpdateCarsViewModel");
GetDisplayData();
Sort.SortOrder(MyList, i => i.Year, false);
_popup = PopupNavigation.Instance;
_modalPage = new PopupPage();
}
public void GetDisplayData()
{
MileageItemRepository repository = new MileageItemRepository();
var response = repository.GetAuto2();
//MyList.Clear();
foreach (var item in response)
{
Analytics.TrackEvent("Car Data GetDisplayData: carId " item.Id " Name = " item.CarDesc " Year = " item.CarYear);
AutoData.Add(new AutoWithSwitch
{
Id = item.Id,
Year = item.CarYear,
Name = item.CarDesc,
IsChecked = item.IsDefault
});
Analytics.TrackEvent("In GetDisplayData in UpdateCarsViewModel CarYear = " item.CarYear);
}
//AutoData = MyList;
}
如您所見,我已經登錄了那里,并且可以看到從 DB 表中檢索到的汽車資訊,它只是不會顯示。
uj5u.com熱心網友回復:
根據您的代碼片段,ListView控制元件系結到ObservableCollection 型別,并且AutoData串列視圖一側的資料將系結到 AutoWithSwitch 類的屬性到四個屬性:
Id、Year和。因此,您應該宣告并使用帶有 set 和 get 屬性的 AutoData,如下所示:NameIsChecked
public class UpdateCarsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<AutoWithSwitch> mylist;
public ObservableCollection<AutoWithSwitch> AutoData
{
get { return mylist; }
set { mylist = value; }
}
public UpdateCarsViewModel()
{
GetDisplayData();
}
public void GetDisplayData()
{
//get data logic
AutoData = new ObservableCollection<AutoWithSwitch>()
{
new AutoWithSwitch()
{
Id = "1",
Year = "2022",
Name = "Steve_Jobs",
IsChecked = false
}
};
}
}
}
注意:確保您可以從下面成功檢索資料,
MileageItemRepository repository = new MileageItemRepository();
var response = repository.GetAuto2();
uj5u.com熱心網友回復:
這很有幫助并回答了我的問題。希望這就是您回答它有幫助的意思。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452316.html
標籤:IOS xamarin xamarin.forms c#-4.0
