Xamarin.forms Xamarin窗体,将新项动态添加到listview

Xamarin.forms Xamarin窗体,将新项动态添加到listview,xamarin.forms,Xamarin.forms,我在internet上找不到一个解决方案,当单击按钮时,如何在Xamarin forms项目中向listview动态添加新项。我在互联网上得到的唯一东西就是如何从listview动态删除项目 因此,请了解我如何在Xamarin表单中编写代码,以便在单击按钮时将新项动态添加到listview?如果列表的ItemSource是ObservableCollection,则只需向集合中添加一项即可更新列表 ObservableCollection<string> data = new Obs

我在internet上找不到一个解决方案,当单击按钮时,如何在Xamarin forms项目中向listview动态添加新项。我在互联网上得到的唯一东西就是如何从listview动态删除项目


因此,请了解我如何在Xamarin表单中编写代码,以便在单击按钮时将新项动态添加到listview?

如果列表的ItemSource是ObservableCollection,则只需向集合中添加一项即可更新列表

ObservableCollection<string> data = new ObservableCollection<string>();

data.Add("a");
data.Add("b");
data.Add("c");

myListView.ItemSource = data;

如果列表的ItemSource是ObservableCollection,只需向集合中添加一个项即可更新列表

ObservableCollection<string> data = new ObservableCollection<string>();

data.Add("a");
data.Add("b");
data.Add("c");

myListView.ItemSource = data;

在MainPage.xaml.cs的代码behinde中,假设您有一个class Person

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

private ObservableCollection<Person> _persons;
public ObservableCollection<Person> Persons
{
    get
    {
        return _persons ?? (_persons = new ObservableCollection<Person>());
    }
}
MainPage.xaml页面如下所示:

private void Button_OnClicked(object sender, EventArgs e)
{
    //create person here 
    var person = new Person()
    {
        Name = "toumir",
        Age = 25
    };

    //add the created person to the list
    Persons.Add(person);
}
<?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:App2"
             x:Class="App2.MainPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackLayout Grid.Row="0">
            <Button Clicked="Button_OnClicked" Text="Add Person"/>
        </StackLayout>

        <ListView Grid.Row="1" ItemsSource="{Binding Persons}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                         <StackLayout Margin="1">
                            <Label Text="{Binding Name}"/>
                            <Label Text="{Binding Age}"/>
                        </StackLayout>
                        <ViewCell.ContextActions>
                            <MenuItem Text="test"></MenuItem>
                        </ViewCell.ContextActions>
                        </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</ContentPage>

在MainPage.xaml.cs的代码behinde中,假设您有一个班级人员

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

private ObservableCollection<Person> _persons;
public ObservableCollection<Person> Persons
{
    get
    {
        return _persons ?? (_persons = new ObservableCollection<Person>());
    }
}
MainPage.xaml页面如下所示:

private void Button_OnClicked(object sender, EventArgs e)
{
    //create person here 
    var person = new Person()
    {
        Name = "toumir",
        Age = 25
    };

    //add the created person to the list
    Persons.Add(person);
}
<?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:App2"
             x:Class="App2.MainPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackLayout Grid.Row="0">
            <Button Clicked="Button_OnClicked" Text="Add Person"/>
        </StackLayout>

        <ListView Grid.Row="1" ItemsSource="{Binding Persons}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                         <StackLayout Margin="1">
                            <Label Text="{Binding Name}"/>
                            <Label Text="{Binding Age}"/>
                        </StackLayout>
                        <ViewCell.ContextActions>
                            <MenuItem Text="test"></MenuItem>
                        </ViewCell.ContextActions>
                        </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</ContentPage>


但我发现,当列表或集合值在运行时更改时,有时UI上的ListView不会更新。我可以通过设置ItemSource=null,然后将ItemSource重新分配给列表来解决此问题。但我发现,有时当列表或集合值在运行时发生更改时,UI上的ListView不会更新。我可以通过设置ItemSource=null,然后将ItemSource重新分配给列表来解决这个问题。