Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ItemModel : INotifyPropertyChanged
- {
- #region Fields
- private string _Name;
- private string _Description;
- #endregion
- #region Events
- public event PropertyChangedEventHandler PropertyChanged;
- #endregion
- #region Public Properties
- public string Name
- {
- get
- {
- return _Name;
- }
- set
- {
- _Name = value;
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
- }
- }
- public string Description
- {
- get
- {
- return _Description;
- }
- set
- {
- _Description = value;
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Description)));
- }
- }
- #endregion
- #region Public Methods
- public override bool Equals(object obj)
- {
- var itemModel = obj as ItemModel;
- if (itemModel == null)
- {
- return false;
- }
- return Name.Equals(itemModel.Name);
- }
- #endregion
- }
- public class ItemModelProvider : ObservableCollection<ItemModel>
- {
- #region Events
- public event EventHandler ItemsLoaded;
- #endregion
- #region Constructor
- public ItemModelProvider()
- {
- var timer = new Timer(TimerCallback, null, 3000, 0);
- }
- #endregion
- #region Private Methods
- private void TimerCallback(object state)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- Add(new ItemModel { Name = "1", Description = "First" });
- Add(new ItemModel { Name = "2", Description = "Second" });
- Add(new ItemModel { Name = "3", Description = "Third" });
- ItemsLoaded?.Invoke(this, new EventArgs());
- });
- }
- #endregion
- }
- <Grid x:Name="TheGrid">
- <Grid.Resources>
- <ResourceDictionary>
- <local:ItemModelProvider x:Key="items" />
- </ResourceDictionary>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition />
- <RowDefinition Height="100" />
- </Grid.RowDefinitions>
- <ListView x:Name="TheListView" Margin="4" SelectedItem="{Binding ItemModel, Mode=TwoWay}" ItemsSource="{StaticResource items}" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="#EEEEEE" >
- <ListView.ItemTemplate>
- <DataTemplate>
- <ViewCell>
- <Grid >
- <Grid.RowDefinitions>
- <RowDefinition Height="20" />
- <RowDefinition Height="20" />
- </Grid.RowDefinitions>
- <Label Text="{Binding Name}" TextColor="#FF0000EE" VerticalOptions="Center" />
- <Label Text="{Binding Description}" Grid.Row="1" VerticalOptions="Center" />
- </Grid>
- </ViewCell>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- <ActivityIndicator x:Name="TheActivityIndicator" IsRunning="True" IsVisible="True" Margin="100" />
- <StackLayout Grid.Row="1" Orientation="Horizontal">
- <Label Text="Name: " />
- <Label Text="{Binding ItemModel.Name}" />
- <Label Text="Description: " />
- <Label Text="{Binding ItemModel.Description}" />
- <Button Text="New Model" x:Name="NewModelButton" />
- <Button Text="Set To 2" x:Name="SetToTwoButton" />
- </StackLayout>
- </Grid>
- public partial class AsyncListViewPage : ContentPage
- {
- ItemModelProvider items;
- ItemModel two;
- private AsyncListViewModel CurrentAsyncListViewModel => BindingContext as AsyncListViewModel;
- public AsyncListViewPage()
- {
- InitializeComponent();
- CreateNewModel();
- items = (ItemModelProvider)TheGrid.Resources["items"];
- items.ItemsLoaded += Items_ItemsLoaded;
- NewModelButton.Clicked += NewModelButton_Clicked;
- SetToTwoButton.Clicked += SetToTwoButton_Clicked;
- }
- private void SetToTwoButton_Clicked(object sender, System.EventArgs e)
- {
- if (two == null)
- {
- DisplayAlert("Wait for the items to load", "Wait for the items to load", "OK");
- return;
- }
- CurrentAsyncListViewModel.ItemModel = two;
- }
- private void NewModelButton_Clicked(object sender, System.EventArgs e)
- {
- CreateNewModel();
- }
- private void CreateNewModel()
- {
- BindingContext = new AsyncListViewModel { ItemModel = new ItemModel { Name = "2", Description = "Second" } };
- }
- private void Items_ItemsLoaded(object sender, System.EventArgs e)
- {
- TheActivityIndicator.IsRunning = false;
- TheActivityIndicator.IsVisible = false;
- two = items[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement