gpt4 book ai didi

c# - 为什么在 .NET MAUI 中删除项目后 ListView 显示不正确?

转载 作者:行者123 更新时间:2023-12-05 05:30:09 25 4
gpt4 key购买 nike

包含所有笔记的第一屏。

在我删除了第一个笔记之后!

我最近开始使用 .NET MAUI 进行编程。 C# 列表中的元素已正确删除。但是,删除后剩余的元素仅部分显示。这意味着只有例如显示第 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);
}
}

如果有任何帮助,我将不胜感激:)

最佳答案

要删除项目,因为你使用的是ListView,你可以删除via按钮点击。但是,您需要按顺序从下到上删除该项目。

代码隐藏:

public partial class MainPage : ContentPage 
{



public ObservableCollection<Note> Notes { get; private set; } = new ObservableCollection<Note>();



public MainPage()
{
InitializeComponent();

AddNotes();

BindingContext = this;
}

private void AddNotes()
{
Notes.Add(new Note("0", "My Note1"));
Notes.Add(new Note("1", "My Note2"));
Notes.Add(new Note("2", "My Note3"));
Notes.Add(new Note("3", "My Note4"));
}

private void Button_Clicked(object sender, EventArgs e)
{
var note = (Button)sender;


Note listnote = (from itm in Notes
where itm.Id == note.CommandParameter.ToString()

select itm).FirstOrDefault<Note>();

Notes.Remove(listnote);
}


}

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"

ItemsSource="{Binding Notes}" >

<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}"

BorderWidth="2"

TextColor="Black"

FontSize="28"

Margin="20"

CornerRadius="100"

WidthRequest="350"

HeightRequest="70"

HorizontalOptions="Center"

VerticalOptions="Start"/>



<Button



Text="Delete"
Clicked="Button_Clicked"
CommandParameter="{Binding Id}"

BorderWidth="2"

BorderColor="Black"

BackgroundColor="White"

WidthRequest="45"

HeightRequest="45"

CornerRadius="35"

Margin="0,0,260,0"

/>


</Grid>

</ViewCell>
</DataTemplate>

</ListView.ItemTemplate>

</ListView>

</Grid>

</StackLayout>

</ScrollView>

</VerticalStackLayout>



注意:

 public class Note 
{
public string Id { get; set; }
public string Title { get; set; }

public Note(string id, string title)
{
Id = id;
Title = title;
}

}

关于c# - 为什么在 .NET MAUI 中删除项目后 ListView 显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74772754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com