简体   繁体   English

Xamarin.forms 列表视图项目源未更新

[英]Xamarin forms list view items source not updating

I am having a problem with my xamarin.forms list view.我的 xamarin.forms 列表视图有问题。 I have created a list view that has an items source that is binded to a List of a custom class. In the app.xaml.cs, I have the exact same list.我创建了一个列表视图,它有一个绑定到自定义列表 class 的项目源。在 app.xaml.cs 中,我有完全相同的列表。 In another form, I am updating the list that resides in app.xaml.cs.在另一种形式中,我正在更新驻留在 app.xaml.cs 中的列表。 When the first form with the list view is brought back up, on appearing I set the local list that is binded to the list in app.xaml.cs.当带列表视图的第一个表单被恢复时,在出现时我设置绑定到 app.xaml.cs 中的列表的本地列表。 But, the items in the list view UI dont update.但是,列表视图 UI 中的项目不会更新。 Can someone please help me with this?有人可以帮我吗?

MainPage.xaml (with list view): MainPage.xaml(带列表视图):

` `

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TrackExpensesApp.MainPage">

        <ContentPage.ToolbarItems>
            <ToolbarItem Text="+"
                        Clicked="OnAddExpenseClicked" />
        </ContentPage.ToolbarItems>

        

    <ListView x:Name="ExpenseEntriesListView"
                        ItemsSource="{Binding ExpenseEntries}"
                        SelectionMode="Single"
                        ItemSelected="ExpenseEntriesItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Padding="10"
                                RowDefinitions="Auto, *"
                                ColumnDefinitions="Auto, *">
                    <Label Grid.ColumnSpan="2"
                                    Text="{Binding Name}"
                                    VerticalOptions="End" />
                    <Label Grid.Row="1"
                                    Text="{Binding DateCreated}"
                                    VerticalOptions="End" />
                    <Label Grid.Row="1"
                                    Grid.Column="1"
                                    Text="{Binding Category}"
                                    VerticalOptions="End" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

` MainPage.xaml.cs (with list view): ` MainPage.xaml.cs(带列表视图):

` `

public partial class MainPage : ContentPage
    {
        public IList<ExpenseEntry> ExpenseEntries { get; set; }

        public MainPage()
        {
            InitializeComponent();
        }

        async void OnAddExpenseClicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new ExpensesEntryPage());
        }

        protected override void OnAppearing()
        {
            ExpenseEntries = (Application.Current as TrackExpensesApp.App).ExpenseEntries;
            
            BindingContext = this;
        }

        async void ExpenseEntriesItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            ExpenseEntry selectedItem = e.SelectedItem as ExpenseEntry;

            await Navigation.PushAsync(new ExpensePage());
        }
    }

` `

App.xaml.cs:应用程序.xaml.cs:

` `

public partial class App : Application
    {
        public IList<ExpenseEntry> ExpenseEntries { get; set; }
        public IList<string> Categories { get; set; }

        public App()
        {
            InitializeComponent();

            // Load in all expenses before loading in the main page
            ExpenseEntries = new List<ExpenseEntry>();

            Categories = new List<string>();
            Categories.Add("Monthly");
            Categories.Add("Vacation");
            Categories.Add("Other");

            MainPage = new NavigationPage(new MainPage());

            BindingContext = this;
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }

` I would truly appreciate it if someone would get back to me as soon as possible. ` 如果有人尽快回复我,我将不胜感激。 Thank you!谢谢你!

We need to call OnPropertyChanged in setter method to notify that a change happened on a property,so that the UI would change after then.我们需要在setter方法中调用OnPropertyChanged来通知属性发生了变化,以便 UI 在此之后发生变化。

MainPage主页

Change your code as below更改您的代码如下

private IList<string> expenseEntries;
public IList<string> ExpenseEntries { 
  get
  {
      return expenseEntries;
  }
  set 
  {
      expenseEntries = value;
      OnPropertyChanged();  //add this line
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM