Exemplo n.º 1
0
        /// <summary>
        /// Shows the window as a dialog.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result produced by the dialog.
        /// </typeparam>
        /// <returns>.
        /// A task that can be used to retrive the result of the dialog when it closes.
        /// </returns>
        public Task <TResult> ShowDialog <TResult>()
        {
            s_windows.Add(this);

            EnsureInitialized();
            IsVisible = true;
            LayoutManager.Instance.ExecuteInitialLayoutPass(this);

            using (BeginAutoSizing())
            {
                var affectedWindows = s_windows.Where(w => w.IsEnabled && w != this).ToList();
                var activated       = affectedWindows.Where(w => w.IsActive).FirstOrDefault();
                SetIsEnabled(affectedWindows, false);

                var modal  = PlatformImpl.ShowDialog();
                var result = new TaskCompletionSource <TResult>();

                Observable.FromEventPattern <EventHandler, EventArgs>(
                    x => this.Closed += x,
                    x => this.Closed -= x)
                .Take(1)
                .Subscribe(_ =>
                {
                    modal.Dispose();
                    SetIsEnabled(affectedWindows, true);
                    activated?.Activate();
                    result.SetResult((TResult)_dialogResult);
                });

                return(result.Task);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Shows the window as a dialog.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result produced by the dialog.
        /// </typeparam>
        /// <returns>.
        /// A task that can be used to retrive the result of the dialog when it closes.
        /// </returns>
        public Task <TResult> ShowDialog <TResult>()
        {
            LayoutManager.ExecuteLayoutPass();

            using (BeginAutoSizing())
            {
                var modal  = PlatformImpl.ShowDialog();
                var result = new TaskCompletionSource <TResult>();

                Observable.FromEventPattern(this, nameof(Closed))
                .Take(1)
                .Subscribe(_ =>
                {
                    modal.Dispose();
                    result.SetResult((TResult)_dialogResult);
                });

                return(result.Task);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Shows the window as a dialog.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result produced by the dialog.
        /// </typeparam>
        /// <param name="owner">The dialog's owner window.</param>
        /// <returns>.
        /// A task that can be used to retrieve the result of the dialog when it closes.
        /// </returns>
        public Task <TResult> ShowDialog <TResult>(IWindowImpl owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }

            if (IsVisible)
            {
                throw new InvalidOperationException("The window is already being shown.");
            }

            RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));

            EnsureInitialized();
            IsVisible = true;
            LayoutManager.ExecuteInitialLayoutPass(this);

            var result = new TaskCompletionSource <TResult>();

            using (BeginAutoSizing())
            {
                PlatformImpl?.ShowDialog(owner);

                Renderer?.Start();
                Observable.FromEventPattern <EventHandler, EventArgs>(
                    x => this.Closed += x,
                    x => this.Closed -= x)
                .Take(1)
                .Subscribe(_ =>
                {
                    owner.Activate();
                    result.SetResult((TResult)(_dialogResult ?? default(TResult)));
                });
                OnOpened(EventArgs.Empty);
            }

            SetWindowStartupLocation(owner);
            return(result.Task);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Shows the window as a dialog.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result produced by the dialog.
        /// </typeparam>
        /// <returns>.
        /// A task that can be used to retrieve the result of the dialog when it closes.
        /// </returns>
        public Task <TResult> ShowDialog <TResult>()
        {
            if (IsVisible)
            {
                throw new InvalidOperationException("The window is already being shown.");
            }

            AddWindow(this);

            EnsureInitialized();
            SetWindowStartupLocation();
            IsVisible = true;
            LayoutManager.ExecuteInitialLayoutPass(this);

            using (BeginAutoSizing())
            {
                var affectedWindows = Application.Current.Windows.Where(w => w.IsEnabled && w != this).ToList();
                var activated       = affectedWindows.Where(w => w.IsActive).FirstOrDefault();
                SetIsEnabled(affectedWindows, false);

                var modal  = PlatformImpl?.ShowDialog();
                var result = new TaskCompletionSource <TResult>();

                Renderer?.Start();

                Observable.FromEventPattern <EventHandler, EventArgs>(
                    x => this.Closed += x,
                    x => this.Closed -= x)
                .Take(1)
                .Subscribe(_ =>
                {
                    modal?.Dispose();
                    SetIsEnabled(affectedWindows, true);
                    activated?.Activate();
                    result.SetResult((TResult)(_dialogResult ?? default(TResult)));
                });

                return(result.Task);
            }
        }