将数据从 Azure/IEnumerable 加载到 ListBox

本文关键字:加载 ListBox IEnumerable 数据 Azure | 更新日期: 2023-09-27 18:35:25

基本问题,我想从 Azure 服务器加载数据并将其发送到 XAML。可以是ListBox或LongListSelector(最佳选择)。你能纠正我的代码吗?我做错了什么?

从 Azure 加载数据并将其发送到 IEnumerable 集合。

视图模型

             public class ViewModel : INotifyPropertyChanged
                {
                    private readonly ICommand _loadDataCommand;
                    private MobileServiceCollection<ModelAzure, ModelAzure> _items;
                    public event PropertyChangedEventHandler PropertyChanged;
                    public IEnumerable itemsControl;
              private async void RefreshTodoItems()
                {
                    try
                    {
                        _items = await _todoTable.ToCollectionAsync();
                    }
                    catch (MobileServiceInvalidOperationException e)
                    {
                        MessageBox.Show(e.Message, "Error loading items, please try again :)", MessageBoxButton.OK);
                    }
                   itemsControl = _items;
                }

                public ICommand LoadDataCommand
                {
                    get { return this._loadDataCommand; }
                }
                private void LoadDataAction(object p)
                {
                    RefreshTodoItems();
                }
                public IEnumerable AzureData
                {
                    get
                    {
                        return itemsControl;
                    }
                    set
                    {
                        itemsControl = value;
                        RaisePropertyChanged("AzureData");
                    }
                }
         private void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
}

XAML

<ListBox Grid.Column="0" Margin="5"
                 ItemsSource="{Binding AzureData}"
                 DisplayMemberPath="Text" Height="180"
                 />

将数据从 Azure/IEnumerable 加载到 ListBox

你需要使用 setter. 而不是私有支持变量来设置 AzureData,以便触发 RaisePropertyChanged 事件。

AzureData = _items;