Ejemplo n.º 1
0
        public static IApp Create(Action <IApp> work, Action <IApp, Exception> onFailure)
        {
            var context  = new ProcessFilesApp();
            var app      = new IoWindowViewModel(context, work, onFailure);
            var ioWindow = new IoWindow(app);

            app.Owner        = ioWindow;
            ioWindow.Closed += (o, e) => context.ExitThread();
            ElementHost.EnableModelessKeyboardInterop(ioWindow);
            ioWindow.Show();
            return(app);
        }
Ejemplo n.º 2
0
        internal IoWindowViewModel(ProcessFilesApp context, Action <IApp> work, Action <IApp, Exception> onFailure)
        {
            this.context = context;
            var isInProgress = this.WhenAnyValue(me => me.TokenSource).Select(source => source != null).ObserveOn(Dispatcher.CurrentDispatcher);

            isInProgressHelper = this.WhenAnyValue(me => me.TokenSource).Select(source => source != null).ToProperty(this, me => me.IsInProgress, true);
            OpenFile           = new InputFileBrowseViewModel(isInProgress.Select(f => !f));
            SaveFile           = new OutputFileBrowseViewModel(isInProgress.Select(f => !f), OpenFile);
            worker.DoWork     += (o, e) =>
            {
                work(this);
                e.Result = 0;
            };
            worker.RunWorkerCompleted += (o, e) =>
            {
                if (TokenSource.IsCancellationRequested)
                {
                    Progress = 0;
                }
                else if (e.Error != null)
                {
                    MessageBox.Show(Owner, e.Error.Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
                    Progress = 0;
                }
                else
                {
                    new ExportCompletedWindow(new ExportCompletedViewModel(Title, SaveFile.File))
                    {
                        Owner = Owner,
                        WindowStartupLocation = WindowStartupLocation.CenterOwner
                    }.ShowDialog();
                }
                TokenSource = null;
                if (e.Error != null)
                {
                    onFailure?.Invoke(this, e.Error);
                }
            };
            worker.ProgressChanged += (o, e) => Progress = e.ProgressPercentage;
            StartCommand            = ReactiveCommand.Create(this.WhenAnyValue(me => me.OpenFile.File, me => me.SaveFile.File, me => me.TokenSource)
                                                             .Select(CanStart).ObserveOn(Dispatcher.CurrentDispatcher));
            CancelCommand = ReactiveCommand.Create(isInProgress);
            StartCommand.Subscribe(_ =>
            {
                TokenSource = new CancellationTokenSource();
                worker.RunWorkerAsync();
            });
            CancelCommand.Subscribe(_ => Cancel());
            isInProgress.Where(f => f).Subscribe(source => CancellationToken = TokenSource.Token);
            this.WhenAnyValue(me => me.Title).Where(title => !string.IsNullOrEmpty(title)).Subscribe(title => Owner.Title = title);
        }