Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. public class ItemModel : INotifyPropertyChanged
  2. {
  3. #region Fields
  4. private string _Name;
  5. private string _Description;
  6. #endregion
  7.  
  8. #region Events
  9. public event PropertyChangedEventHandler PropertyChanged;
  10. #endregion
  11.  
  12. #region Public Properties
  13. public string Name
  14. {
  15. get
  16. {
  17. return _Name;
  18. }
  19.  
  20. set
  21. {
  22. _Name = value;
  23. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
  24. }
  25. }
  26.  
  27. public string Description
  28. {
  29. get
  30. {
  31. return _Description;
  32. }
  33.  
  34. set
  35. {
  36. _Description = value;
  37. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Description)));
  38. }
  39. }
  40. #endregion
  41.  
  42. #region Public Methods
  43. public override bool Equals(object obj)
  44. {
  45. var itemModel = obj as ItemModel;
  46. if (itemModel == null)
  47. {
  48. return false;
  49. }
  50.  
  51. return Name.Equals(itemModel.Name);
  52. }
  53. #endregion
  54. }
  55.  
  56. public class ItemModelProvider : ObservableCollection<ItemModel>
  57. {
  58. #region Events
  59. public event EventHandler ItemsLoaded;
  60. #endregion
  61.  
  62. #region Constructor
  63. public ItemModelProvider()
  64. {
  65. var timer = new Timer(TimerCallback, null, 3000, 0);
  66. }
  67. #endregion
  68.  
  69. #region Private Methods
  70. private void TimerCallback(object state)
  71. {
  72. Device.BeginInvokeOnMainThread(() =>
  73. {
  74. Add(new ItemModel { Name = "1", Description = "First" });
  75. Add(new ItemModel { Name = "2", Description = "Second" });
  76. Add(new ItemModel { Name = "3", Description = "Third" });
  77. ItemsLoaded?.Invoke(this, new EventArgs());
  78. });
  79. }
  80. #endregion
  81. }
  82.  
  83. <Grid x:Name="TheGrid">
  84.  
  85. <Grid.Resources>
  86. <ResourceDictionary>
  87. <local:ItemModelProvider x:Key="items" />
  88. </ResourceDictionary>
  89. </Grid.Resources>
  90.  
  91. <Grid.RowDefinitions>
  92. <RowDefinition />
  93. <RowDefinition Height="100" />
  94. </Grid.RowDefinitions>
  95.  
  96. <ListView x:Name="TheListView" Margin="4" SelectedItem="{Binding ItemModel, Mode=TwoWay}" ItemsSource="{StaticResource items}" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="#EEEEEE" >
  97.  
  98. <ListView.ItemTemplate>
  99. <DataTemplate>
  100.  
  101. <ViewCell>
  102.  
  103. <Grid >
  104.  
  105. <Grid.RowDefinitions>
  106. <RowDefinition Height="20" />
  107. <RowDefinition Height="20" />
  108. </Grid.RowDefinitions>
  109.  
  110. <Label Text="{Binding Name}" TextColor="#FF0000EE" VerticalOptions="Center" />
  111. <Label Text="{Binding Description}" Grid.Row="1" VerticalOptions="Center" />
  112.  
  113. </Grid>
  114.  
  115.  
  116. </ViewCell>
  117.  
  118. </DataTemplate>
  119. </ListView.ItemTemplate>
  120.  
  121. </ListView>
  122.  
  123. <ActivityIndicator x:Name="TheActivityIndicator" IsRunning="True" IsVisible="True" Margin="100" />
  124.  
  125. <StackLayout Grid.Row="1" Orientation="Horizontal">
  126. <Label Text="Name: " />
  127. <Label Text="{Binding ItemModel.Name}" />
  128. <Label Text="Description: " />
  129. <Label Text="{Binding ItemModel.Description}" />
  130. <Button Text="New Model" x:Name="NewModelButton" />
  131. <Button Text="Set To 2" x:Name="SetToTwoButton" />
  132. </StackLayout>
  133.  
  134. </Grid>
  135.  
  136. public partial class AsyncListViewPage : ContentPage
  137. {
  138. ItemModelProvider items;
  139. ItemModel two;
  140.  
  141. private AsyncListViewModel CurrentAsyncListViewModel => BindingContext as AsyncListViewModel;
  142.  
  143. public AsyncListViewPage()
  144. {
  145. InitializeComponent();
  146.  
  147. CreateNewModel();
  148.  
  149. items = (ItemModelProvider)TheGrid.Resources["items"];
  150. items.ItemsLoaded += Items_ItemsLoaded;
  151.  
  152. NewModelButton.Clicked += NewModelButton_Clicked;
  153. SetToTwoButton.Clicked += SetToTwoButton_Clicked;
  154. }
  155.  
  156. private void SetToTwoButton_Clicked(object sender, System.EventArgs e)
  157. {
  158. if (two == null)
  159. {
  160. DisplayAlert("Wait for the items to load", "Wait for the items to load", "OK");
  161. return;
  162. }
  163.  
  164. CurrentAsyncListViewModel.ItemModel = two;
  165. }
  166.  
  167. private void NewModelButton_Clicked(object sender, System.EventArgs e)
  168. {
  169. CreateNewModel();
  170. }
  171.  
  172. private void CreateNewModel()
  173. {
  174. BindingContext = new AsyncListViewModel { ItemModel = new ItemModel { Name = "2", Description = "Second" } };
  175. }
  176.  
  177. private void Items_ItemsLoaded(object sender, System.EventArgs e)
  178. {
  179. TheActivityIndicator.IsRunning = false;
  180. TheActivityIndicator.IsVisible = false;
  181. two = items[1];
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement