簡體   English   中英

Xamarin.forms 列表視圖項目源未更新

[英]Xamarin forms list view items source not updating

我的 xamarin.forms 列表視圖有問題。 我創建了一個列表視圖,它有一個綁定到自定義列表 class 的項目源。在 app.xaml.cs 中,我有完全相同的列表。 在另一種形式中,我正在更新駐留在 app.xaml.cs 中的列表。 當帶列表視圖的第一個表單被恢復時,在出現時我設置綁定到 app.xaml.cs 中的列表的本地列表。 但是,列表視圖 UI 中的項目不會更新。 有人可以幫我嗎?

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(帶列表視圖):

`

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());
        }
    }

`

應用程序.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()
        {
        }
    }

` 如果有人盡快回復我,我將不勝感激。 謝謝你!

我們需要在setter方法中調用OnPropertyChanged來通知屬性發生了變化,以便 UI 在此之后發生變化。

主頁

更改您的代碼如下

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