Exemplo n.º 1
0
        /// <summary>
        /// Adds a Crystal Dialog instance to the specified window and makes it visible asynchronously.
        /// If you want to wait until the user has closed the dialog, use <see cref="CrystalDialogBase.WaitUntilUnloadedAsync"/>
        /// <para>You have to close the resulting dialog yourself with <see cref="HideCrystalDialogAsync"/>.</para>
        /// </summary>
        /// <param name="window">The owning window of the dialog.</param>
        /// <param name="dialog">The dialog instance itself.</param>
        /// <param name="settings">An optional pre-defined settings instance.</param>
        /// <returns>A task representing the operation.</returns>
        /// <exception cref="InvalidOperationException">The <paramref name="dialog"/> is already visible in the window.</exception>
        public static Task ShowCrystalDialogAsync(this CrystalWindow window, CrystalDialogBase dialog, CrystalDialogSettings?settings = null)
        {
            if (window is null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            window.Dispatcher.VerifyAccess();

            if (dialog is null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            if (window.crystalActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.crystalInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            if (window.crystalActiveDialogContainer.Children.Contains(dialog) || window.crystalInactiveDialogContainer.Children.Contains(dialog))
            {
                throw new InvalidOperationException("The provided dialog is already visible in the specified window.");
            }

            settings ??= (dialog.DialogSettings ?? window.CrystalDialogOptions);

            return(HandleOverlayOnShow(settings, window).ContinueWith(z =>
            {
                return window.Dispatcher.Invoke(new Func <Task>(() =>
                {
                    SetDialogFontSizes(settings, dialog);

                    SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog);
                    dialog.SizeChangedHandler = sizeHandler;

                    return dialog.WaitForLoadAsync().ContinueWith(x =>
                    {
                        dialog.OnShown();

                        if (DialogOpened != null)
                        {
                            window.Dispatcher.BeginInvoke(new Action(() => DialogOpened(window, new DialogStateChangedEventArgs())));
                        }
                    });
                }));
            }).Unwrap());
        }
Exemplo n.º 2
0
        private static SizeChangedEventHandler SetupAndOpenDialog(CrystalWindow window, CrystalDialogBase dialog)
        {
            dialog.SetValue(Panel.ZIndexProperty, (int)(window.overlayBox?.GetValue(Panel.ZIndexProperty) ?? 0) + 1);

            var fixedMinHeight = dialog.MinHeight > 0;
            var fixedMaxHeight = dialog.MaxHeight is not double.PositiveInfinity && dialog.MaxHeight > 0;

            void CalculateMinAndMaxHeight()
            {
                if (!fixedMinHeight)
                {
                    dialog.SetCurrentValue(FrameworkElement.MinHeightProperty, window.ActualHeight / 4.0);
                }

                if (!fixedMaxHeight)
                {
                    dialog.SetCurrentValue(FrameworkElement.MaxHeightProperty, window.ActualHeight);
                }
                else
                {
                    dialog.SetCurrentValue(FrameworkElement.MinHeightProperty, Math.Min(dialog.MinHeight, dialog.MaxHeight));
                }
            }

            CalculateMinAndMaxHeight();

            void OnWindowSizeChanged(object sender, SizeChangedEventArgs args)
            {
                CalculateMinAndMaxHeight();
            }

            window.SizeChanged += OnWindowSizeChanged;

            window.AddDialog(dialog);

            dialog.OnShown();

            return(OnWindowSizeChanged);
        }