Ejemplo n.º 1
0
        public static void SetupDataContextChanged <T>(this FrameworkElement element, Action <T> action) where T : class
        {
            var context = element.DataContext;

            if (context != null)
            {
                if (action != null)
                {
                    action.Invoke(context as T);
                }
                return;
            }

            var cleanup = new DisposeManager();

            DependencyPropertyChangedEventHandler handler = (s, e) =>
            {
                cleanup.Dispose();
                if (action != null)
                {
                    action.Invoke(e.NewValue as T);
                }
            };

            cleanup.AddEvent(h => element.DataContextChanged += h, h => element.DataContextChanged -= h, handler);
        }
Ejemplo n.º 2
0
        public static Task ShutdownAsync(this Dispatcher d)
        {
            var tcs = new TaskCompletionSource <int>();

            var cleanup = new DisposeManager();

            EventHandler handler = (s, e) =>
            {
                cleanup.Dispose();
                tcs.TrySetResult(0);
            };

            cleanup.AddEvent(h => d.ShutdownFinished += h, h => d.ShutdownFinished -= h, handler);
            d.BeginInvokeShutdown(DispatcherPriority.Normal);

            return(tcs.Task);
        }
        private void HandleNavigateTo(IFileSystemFolderOperations operation, TFolder selected)
        {
            if (_gridSelectionToken != null)
            {
                _gridSelectionToken.Dispose();
            }

            var gvm = _gridViewModel;

            EventHandler handler = (s, e) => gvm.SetSelectedAsync(selected as TFile);

            var token = _gridSelectionToken = new DisposeManager();

            token.AddEvent(h => gvm.FilesPopulated += h, h => gvm.FilesPopulated -= h, handler);

            _treeViewModel.SelectAsync(operation.DriveFolder);
        }
Ejemplo n.º 4
0
        public static void SetupLoaded(this FrameworkElement element, Action action)
        {
            if (element.IsLoaded)
            {
                action();
                return;
            }

            var cleanup = new DisposeManager();

            RoutedEventHandler handler = (s, e) =>
            {
                cleanup.Dispose();
                action();
            };

            cleanup.AddEvent(h => element.Loaded += h, h => element.Loaded -= h, handler);
        }
Ejemplo n.º 5
0
 private void HandleCompletedOrCanceled(DisposeManager cleanup, Window window, ProgressViewModel viewModel, IProgressItemViewModel <T> itemViewModel, bool isCompleted)
 {
     cleanup.Dispose();
     window.Dispatcher.CheckedInvoke(() =>
     {
         viewModel.ProgressItems.Remove(itemViewModel);
         if (viewModel.ProgressItems.Count == 0)
         {
             Task.Delay(1000);
             if (_window != null && _window.IsVisible)
             {
                 viewModel.OkCommand.TryExecute(null);
             }
             _window    = null;
             var result = new AsyncActivityResult <IProgressItemViewModel <T> >(isCompleted, itemViewModel);
             Completion.TrySetResult(result);
         }
     });
 }
Ejemplo n.º 6
0
        private async Task NavigateAsync(IFolder folder, TFolder selected)
        {
            _treeViewModel.PropertyChanged -= HandleTreePropertyChanged;

            if (_gridSelectionToken != null)
            {
                _gridSelectionToken.Dispose();
            }

            var gvm = _gridViewModel;

            EventHandler handler = (s, e) => gvm.SetSelectedAsync(selected as TFile);

            var token = _gridSelectionToken = new DisposeManager();

            token.AddEvent(h => gvm.FilesPopulated += h, h => gvm.FilesPopulated -= h, handler);

            await _treeViewModel.SelectAsync(folder);

            _treeViewModel.PropertyChanged += HandleTreePropertyChanged;
        }