.NET MAUI中删除ListView项后显示异常的问题求助
.NET MAUI ListView 删除元素后显示异常问题
显示所有笔记的初始界面:
删除第一条笔记后的界面:
最近开始用.NET MAUI开发,C#中的ObservableCollection列表已正确移除元素,但删除后剩余元素仅部分显示,例如仅第4个元素正常显示,其余仅显示空栏。
我的代码如下:
XAML代码
<VerticalStackLayout> <ScrollView> <StackLayout> <Grid> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Image Grid.Column="0" Source="logo.png" WidthRequest="150" HorizontalOptions="Start" VerticalOptions="Start"/> <Label TextColor="Black" Grid.Column="1" Text="TODO" FontSize="35" HorizontalOptions="Start" VerticalOptions="Start" Margin="23"/> </Grid> <Grid BackgroundColor="#24D4A3"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <ListView Grid.ColumnSpan="2" Grid.RowSpan="2" RowHeight="100" x:Name="listview"> <ListView.ItemTemplate> <DataTemplate > <ViewCell > <Grid BackgroundColor="#24D4A3"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <Button BackgroundColor="#DEABF5" Text="{Binding Title}" Clicked="onNoteSelected" BorderWidth="2" TextColor="Black" FontSize="28" Margin="20" CornerRadius="100" WidthRequest="350" HeightRequest="70" HorizontalOptions="Center" VerticalOptions="Start"/> <Button BindingContext="{Binding Id}" Clicked="ToDoSolved" BorderWidth="2" BorderColor="Black" BackgroundColor="White" WidthRequest="45" HeightRequest="45" CornerRadius="35" Margin="0,0,260,0" /> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> <ImageButton Clicked="Settings" Source="settings.png" Grid.Row="0" Grid.Column="2" BorderColor="#2b3c3c" BorderWidth="0" BackgroundColor="#34A4EB" CornerRadius="35" HorizontalOptions="End" WidthRequest="70" HeightRequest="70" Margin="0,10, 10, 0" VerticalOptions="Start"/> <ImageButton Clicked="CreateNote" Source="add.png" Grid.Row="1" Grid.Column="2" BorderColor="#2b3c3c" BorderWidth="0" BackgroundColor="#34A4EB" CornerRadius="35" HorizontalOptions="End" WidthRequest="70" HeightRequest="70" Margin="0,0,10,10" Padding="2,0,0,0"/> </Grid> </StackLayout> </ScrollView> </VerticalStackLayout>
C#代码
public partial class MainPage : ContentPage { private ObservableCollection<Note> notes = new ObservableCollection<Note>(); public ObservableCollection<Note> Notes { get { return notes; } set { notes = value; } } public MainPage() { InitializeComponent(); notes.Add(new Note(1, "My Note1", "I'm ugly")); notes.Add(new Note(2, "My Note2", "I'm short")); notes.Add(new Note(3, "My Note3", "I'm smart")); notes.Add(new Note(4, "My Note4", "I'm smart")); //notes.Add(new Note(6, "My Note6", "I'm smart")); //notes.Add(new Note(7, "My Note7", "I'm smart")); //notes.Add(new Note(8, "My Note8", "I'm smart")); //notes.Add(new Note(9, "My Note9", "I'm smart")); this.BindingContext= Notes; listview.ItemsSource= Notes; } private async void CreateNote(object sender, EventArgs e) { await Shell.Current.GoToAsync("//CreateNote"); } private async void Settings(object sender, EventArgs e) { await Shell.Current.GoToAsync("//Settings"); } private void ToDoSolved(object sender, EventArgs e) { Button button= (Button) sender ; var id = (int)button.BindingContext; var item = Notes.SingleOrDefault(x => x.Id == id); if (item != null) { Notes.Remove(item); Console.WriteLine(id); } } async void onNoteSelected(object sender, EventArgs e) { Button button= (Button) sender ; var id = (int)button.BindingContext; //await Shell.Current.GoToAsync("NotePage" + id); } }
恳请各位提供帮助 :)
内容的提问来源于stack exchange,提问作者Der Illuminate HD
相关产品推荐
相关产品推荐

