예제 #1
0
        private void AddProgressItem(Window window, IProgressItemViewModel <T> itemViewModel)
        {
            var viewModel = (ProgressViewModel)window.DataContext;

            viewModel.ProgressItems.Add(itemViewModel);

            var cleanup = new DisposeManager();

            cleanup.AddEvent(h => itemViewModel.Completed += h, h => itemViewModel.Completed -= h,
                             (EventHandler)((s, e) => HandleCompletedOrCanceled(cleanup, window, viewModel, itemViewModel, true)));
            cleanup.AddEvent(h => itemViewModel.Canceled += h, h => itemViewModel.Canceled -= h,
                             (EventHandler)((s, e) => HandleCompletedOrCanceled(cleanup, window, viewModel, itemViewModel, false)));
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #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);
        }