示例#1
0
文件: WindowView.cs 项目: uxifiit/UXC
 public Task ShowAsync()
 {
     return(AsyncHelper.InvokeAsync <EventHandler>(
                Show,
                h => _window.Closed += h,
                h => _window.Closed -= h,
                tcs => (s, e) => tcs.TrySetResult(true)));
 }
示例#2
0
        private async Task CalibrateAsync(ICalibrator calibrator)
        {
            var execution = calibrator as IEyeTrackerCalibrationExecution;

            var result = SessionStepResult.Failed;

            using (var cancellation = new CancellationDisposable())
            {
                _cancellation = cancellation;

                try
                {
                    execution.CalibrationFinished += Execution_CalibrationFinished;

                    await AsyncHelper.InvokeAsync <CalibratorStateChangedEventHandler, bool>
                    (
                        () => { },
                        h => calibrator.StateChanged += h,
                        h => calibrator.StateChanged -= h,
                        tcs => (c, state) =>
                    {
                        switch (state)
                        {
                        case CalibratorState.Canceled:
                            tcs.TrySetResult(false);
                            break;

                        case CalibratorState.Completed:
                            tcs.TrySetResult(true);
                            break;
                        }
                    },
                        cancellation.Token
                    );

                    // create result with calibrations
                    result = new EyeTrackerCalibrationSessionStepResult(_calibrations);
                }
                catch (OperationCanceledException)
                {
                    await calibrator.CancelAsync();
                }
                finally
                {
                    //remove handler from execution
                    if (execution != null)
                    {
                        execution.CalibrationFinished -= Execution_CalibrationFinished;
                    }
                }

                Navigation.Clear();

                _cancellation = null;
            }

            OnCompleted(result);
        }
示例#3
0
 public static async Task WaitUntilLoadedAsync(this FrameworkElement element)
 {
     await AsyncHelper.InvokeAsync <RoutedEventHandler>
     (
         () => { },
         handler => element.Loaded += handler,
         handler => element.Loaded -= handler,
         tcs => (o, args) => tcs.SetResult(true)
     );
 }
示例#4
0
文件: FlyoutView.cs 项目: uxifiit/UXC
        public Task ShowAsync()
        {
            var flyout = Flyout;

            flyout.Position = (Position)Position;
            return(AsyncHelper.InvokeAsync <RoutedEventHandler>(
                       Show,
                       h => flyout.ClosingFinished += h,
                       h => flyout.ClosingFinished -= h,
                       tcs => (s, e) => tcs.TrySetResult(true)));
        }
示例#5
0
        private async Task TryStartCalibrationAsync(ICalibrate calibrate)
        {
            if (CanCalibrate == false)
            {
                using (var cancellation = new CancellationDisposable())
                {
                    _cancellation = cancellation;

                    await AsyncHelper.InvokeAsync <EventHandler <bool>, bool>
                    (
                        () => { },
                        h => calibrate.CanCalibrateChanged += h,
                        h => calibrate.CanCalibrateChanged -= h,
                        tcs => (_, canCalibrate) =>
                    {
                        if (canCalibrate)
                        {
                            tcs.TrySetResult(canCalibrate);
                        }
                    },
                        cancellation.Token
                    );

                    OnPropertyChanged(nameof(CanCalibrate));

                    _cancellation = null;
                }
            }

            if (CanCalibrate)
            {
                var calibrator = calibrate.GetCalibrator();

                var calibratorViewModel = (CalibratorViewModel)_resolver.Create(calibrator);

                ApplySettings(calibratorViewModel);

                Navigation.NavigateToObject(calibratorViewModel);

                await CalibrateAsync(calibrator);
            }
            else
            {
                Complete();
            }
        }
示例#6
0
 public static async Task RunAsync(this Storyboard storyboard, CancellationToken cancellationToken)
 {
     try
     {
         await AsyncHelper.InvokeAsync <EventHandler>(
             () => storyboard.Begin(),
             handler => storyboard.Completed += handler,
             handler => storyboard.Completed -= handler,
             tcs => (s, e) => tcs.TrySetResult(true),
             cancellationToken);
     }
     catch (OperationCanceledException)
     {
         storyboard.Stop();
         throw;
     }
 }
示例#7
0
        public async Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            string text = String.IsNullOrWhiteSpace(message.Body)
                        ? message.Subject
                        : String.Format("Please click on this link to {0}: {1}", message.Subject.ToLower(), message.Body);
            string html = String.IsNullOrWhiteSpace(message.Body)
                        ? message.Subject
                        : String.Format("Please click on this link to {0}: <a href=\"{1}\">link</a><br/><br/>Or copy and open the following link in the browser:<br/>{1}", message.Subject.ToLower(), message.Body);

            using (var client = new SmtpClient())
                using (var mail = new MailMessage())
                {
                    mail.To.Add(new MailAddress(message.Destination));
                    mail.Subject = "[UXR] " + message.Subject;
                    mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                    mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

                    await AsyncHelper.InvokeAsync <SendCompletedEventHandler>
                    (
                        () => client.SendAsync(mail, null),
                        h => client.SendCompleted += h,
                        h => client.SendCompleted -= h,
                        (tcs) => (s, e) =>
                    {
                        if (e.Cancelled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else if (e.Error != null)
                        {
                            tcs.TrySetException(e.Error);
                        }
                        else
                        {
                            tcs.TrySetResult(true);
                        }
                    }
                    );
                }
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// <remarks>See Tobii docs p. 20.</remarks>
        private async Task RunCalibrationAsync()
        {
            bool cancelled   = false;
            bool failed      = false;
            var  calibration = _calibration;

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                KeyEventHandler    keyCancelEventHandler = CreateKeyCancelEventHandler(cts);
                CancelEventHandler cancelEventHandler    = CreateCancelEventHandler(cts);

                try
                {
                    if (_window != null)
                    {
                        _window.KeyDown += keyCancelEventHandler;
                        _window.Closing += cancelEventHandler;
                    }

                    VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(IntroductionState), true);
                    await Task.Delay(3000, cts.Token);

                    VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(CalibrationProcessState), true);
                    await Task.Delay(300, cts.Token);

                    var failedTask = AsyncHelper.InvokeAsync <EventHandler <Exception> >
                                     (
                        () => { },
                        h => calibration.Failed += h,
                        h => calibration.Failed -= h,
                        tcs => (_, ex) => tcs.TrySetException(ex),
                        cts.Token
                                     );

                    var completedTask = AsyncHelper.InvokeAsync <EventHandler <CalibrationExecutionReport> >
                                        (
                        () => calibration.Start(),
                        h => calibration.Completed += h,
                        h => calibration.Completed -= h,
                        tcs => (_, __) => tcs.TrySetResult(true),
                        cts.Token
                                        );

                    var finishedTask = await Task.WhenAny(failedTask, completedTask);

                    if (finishedTask.IsCanceled)
                    {
                        cancelled = true;
                    }
                    else if (finishedTask == failedTask)
                    {
                        failed = true;
                        // TODO 10/09/2016 add error
                        // show message based on the exception
                    }

                    // TODO 12/10/2016 Show loading if the evaluation takes too long.
                    // await calibration.FinishAsync();
                }
                catch (OperationCanceledException)
                {
                    cancelled = true;
                }
                catch (Exception)
                {
                    failed = true;
                    // Show unknown error
                }
                finally
                {
                    if (_window != null)
                    {
                        _window.KeyDown -= keyCancelEventHandler;
                        _window.Closing -= cancelEventHandler;
                    }
                }
            }

            if (cancelled)
            {
                calibration?.Cancel();
            }
            else if (failed)
            {
                VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(ErrorState), true);
            }
        }