コード例 #1
0
        public MandelbrotViewModel(double baseWidth, double baseHeight)
        {
            this.baseWidth  = baseWidth;
            this.baseHeight = baseHeight;

            // Create MandelbrotModel object.
            MandelbrotModel model = new MandelbrotModel();

            // Progress reporter
            Progress <double> progressReporter = new Progress <double>((double progress) =>
            {
                Progress = progress;
            });

            CancellationTokenSource cancelTokenSource = null;

            // Define CalculateCommand and CancelCommand
            CalculateCommand = new Command(
                execute: async() =>
            {
                // Disable this button and enable Cancel button.
                IsBusy = true;
                ((Command)CalculateCommand).ChangeCanExecute();
                ((Command)CancelCommand).ChangeCanExecute();

                // Create CancellationToken.
                cancelTokenSource             = new CancellationTokenSource();
                CancellationToken cancelToken = cancelTokenSource.Token;

                try
                {
                    // Perform the calculation.
                    BitmapInfo = await model.CalculateAsync(TargetCenter,
                                                            baseWidth / TargetMagnification,
                                                            baseHeight / TargetMagnification,
                                                            PixelWidth, PixelHeight,
                                                            Iterations,
                                                            progressReporter,
                                                            cancelToken);

                    // Processing only for a successful completion.
                    CurrentCenter        = TargetCenter;
                    CurrentMagnification = TargetMagnification;
                    RealOffset           = 0.5;
                    ImaginaryOffset      = 0.5;
                }
                catch (OperationCanceledException)
                {
                    // Operation cancelled!
                }
                catch
                {
                    // Another type of exception? This should not occur.
                }

                // Processing regardless of success or cancellation.
                Progress = 0;
                IsBusy   = false;

                // Disable Cancel button and enable this button.
                ((Command)CalculateCommand).ChangeCanExecute();
                ((Command)CancelCommand).ChangeCanExecute();
            },
                canExecute: () =>
            {
                return(!IsBusy);
            });

            CancelCommand = new Command(
                execute: () =>
            {
                cancelTokenSource.Cancel();
            },
                canExecute: () =>
            {
                return(IsBusy);
            });
        }
コード例 #2
0
        public MandelbrotViewModel(double baseWidth, double baseHeight)
        {
            this.baseWidth = baseWidth;
            this.baseHeight = baseHeight;

            // Create MandelbrotModel object.
            MandelbrotModel model = new MandelbrotModel();

            // Progress reporter
            Progress<double> progressReporter = new Progress<double>((double progress) =>
            {
                Progress = progress;
            });

            CancellationTokenSource cancelTokenSource = null;

            // Define CalculateCommand and CancelCommand
            CalculateCommand = new Command(
                execute: async () =>
                    {
                        // Disable this button and enable Cancel button.
                        IsBusy = true;
                        ((Command)CalculateCommand).ChangeCanExecute();
                        ((Command)CancelCommand).ChangeCanExecute();

                        // Create CancellationToken.
                        cancelTokenSource = new CancellationTokenSource();
                        CancellationToken cancelToken = cancelTokenSource.Token;

                        try
                        {
                            // Perform the calculation.
                            BitmapInfo = await model.CalculateAsync(TargetCenter,
                                                                    baseWidth / TargetMagnification,
                                                                    baseHeight / TargetMagnification,
                                                                    PixelWidth, PixelHeight,
                                                                    Iterations,
                                                                    progressReporter,
                                                                    cancelToken);

                            // Processing only for a successful completion.
                            CurrentCenter = TargetCenter;
                            CurrentMagnification = TargetMagnification;
                            RealOffset = 0.5;
                            ImaginaryOffset = 0.5;
                        }
                        catch (OperationCanceledException)
                        {
                            // Operation cancelled!
                        }
                        catch
                        {
                            // Another type of exception? This should not occur.
                        }

                        // Processing regardless of success or cancellation.
                        Progress = 0;
                        IsBusy = false;

                        // Disable Cancel button and enable this button.
                        ((Command)CalculateCommand).ChangeCanExecute();
                        ((Command)CancelCommand).ChangeCanExecute();
                    },
                canExecute: () =>
                    {
                        return !IsBusy;
                    });

            CancelCommand = new Command(
                execute: () =>
                    {
                        cancelTokenSource.Cancel();
                    },
                canExecute: () =>
                    {
                        return IsBusy;
                    });
        }