Advertisement
jlind0

Untitled

Feb 18th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. public abstract class NavigationItem<T> : NavigationItem
  2.     {
  3.         public NavigationItem(NavigationData data)
  4.             : base(data)
  5.         {
  6.             Items = new ObservableCollection<T>();
  7.         }
  8.         public ObservableCollection<T> Items { get; private set; }
  9.         protected abstract Task<IEnumerable<T>> GetItems(CancellationToken token);
  10.         protected override async Task DoLoad(CancellationToken token)
  11.         {
  12.             var items = await GetItems(token);
  13.             token.ThrowIfCancellationRequested();
  14.             if (items != null)
  15.             {
  16.                 await Task.Run(() =>
  17.                 {
  18.                     DispatcherLocator.Dispatcher.Invoke(() =>
  19.                     {
  20.                         foreach (var item in items)
  21.                         {
  22.                             Items.Add(item);
  23.                             if (token.IsCancellationRequested)
  24.                                 break;
  25.                         }
  26.                     });
  27.                 });
  28.             }
  29.         }
  30.         protected override async Task DoUnload()
  31.         {
  32.             await Task.Run(() => DispatcherLocator.Dispatcher.Invoke(() => Items.Clear()));
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement