Esempio n. 1
0
        internal async Task <object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (IsOpen)
            {
                throw new InvalidOperationException("DialogHost is already open.");
            }


            _dialogTaskCompletionSource = new TaskCompletionSource <object>();

            AssertTargetableContent();

            if (content != null)
            {
                DialogContent = content;
            }

            _asyncShowOpenedEventHandler  = openedEventHandler;
            _asyncShowClosingEventHandler = closingEventHandler;
            SetCurrentValue(IsOpenProperty, true);

            object result = await _dialogTaskCompletionSource.Task;

            _asyncShowOpenedEventHandler  = null;
            _asyncShowClosingEventHandler = null;

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
        /// </summary>
        /// <param name="content">Content to show (can be a control or view model).</param>
        /// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
        /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
        /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
        /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
        public static Task <object> Show(object content, object dialogIdentifier, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (LoadedInstances.Count == 0)
            {
                throw new InvalidOperationException("No loaded DialogHost instances.");
            }
            LoadedInstances.First().Dispatcher.VerifyAccess();

            var targets = LoadedInstances.Where(dh => dialogIdentifier == null || Equals(dh.Identifier, dialogIdentifier)).ToList();

            if (targets.Count == 0)
            {
                throw new InvalidOperationException("No loaded DialogHost have an Identifier property matching dialogIndetifier argument.");
            }
            if (targets.Count > 1)
            {
                throw new InvalidOperationException("Multiple viable DialogHosts.  Specify a unique Identifier on each DialogHost, especially where multiple Windows are a concern.");
            }

            return(targets[0].ShowInternal(content, openedEventHandler, closingEventHandler));
        }
Esempio n. 3
0
        /// <summary>
        /// 弹出窗口
        /// </summary>
        /// <param name="openedEventHandler"></param>
        /// <param name="closingEventHandler"></param>
        /// <returns></returns>
        public async virtual Task <bool> ShowDialog(DialogOpenedEventHandler openedEventHandler = null, DialogClosingEventHandler closingEventHandler = null)
        {
            var    dialog     = GetDialog();
            object taskResult = await DialogHost.Show(dialog, "RootDialog", openedEventHandler, closingEventHandler); //位于顶级窗口

            return((bool)taskResult);
        }
Esempio n. 4
0
        internal Task <object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (IsOpen)
            {
                throw new InvalidOperationException("DialogHost is already open.");
            }

            AssertTargetableContent();
            DialogContent = content;
            _asyncShowOpenedEventHandler  = openedEventHandler;
            _asyncShowClosingEventHandler = closingEventHandler;
            SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                _asyncShowWaitHandle.WaitOne();
            }).ContinueWith(t =>
            {
                _asyncShowOpenedEventHandler  = null;
                _asyncShowClosingEventHandler = null;

                return(_closeDialogExecutionParameter);
            });

            return(task);
        }
Esempio n. 5
0
        public async Task ShowAsync(object o, string dialogID, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            var control = new OperationProcessingControl();

            control.DataContext = o;
            await DialogHost.Show(control, dialogID, openedEventHandler, closingEventHandler);
        }
Esempio n. 6
0
        internal async Task <object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (IsOpen)
            {
                throw new InvalidOperationException("对话框已经被打开。");
            }
            AssertTargetableContent();
            DialogContent = content;
            _asyncShowOpenedEventHandler  = openedEventHandler;
            _asyncShowClosingEventHandler = closingEventHandler;
            SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                _asyncShowWaitHandle.WaitOne();
            });

            task.Start();

            await task;

            _asyncShowOpenedEventHandler  = null;
            _asyncShowClosingEventHandler = null;

            return(_closeDialogExecutionParameter);
        }
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled)
            {
                return;
            }

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;

            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler  = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();
                DialogContent = executedRoutedEventArgs.Parameter;
            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
Esempio n. 8
0
        public override Task <bool> ShowDialog(DialogOpenedEventHandler openedEventHandler = null, DialogClosingEventHandler closingEventHandler = null)
        {
            var dialog = GetDialogWindow();

            dialog.ShowDialog();
            return(Task.FromResult((dialog.DataContext as BaseDialogOperation).Result));
        }
Esempio n. 9
0
 public static Task <object> Show(object content, DialogOpenedEventHandler openedEventHandler = null, DialogClosingEventHandler closingEventHandler = null)
 {
     return(Application.Current.Dispatcher.Invoke(() => {
         CloseDialog();
         return DialogHost.Show(content, openedEventHandler, closingEventHandler);
     }));
 }
Esempio n. 10
0
 public void OpenDialog(INotifyPropertyChanged viewModel, DialogOpenedEventHandler dialogOpenedEventHandler)
 {
     DialogHost.Show(viewModel, (object source, MaterialDesignThemes.Wpf.DialogOpenedEventArgs args) =>
     {
         dialogOpenedEventHandler?.Invoke(source, new DialogOpenedEventArgs(new MDSession(args.Session)));
     });
 }
        internal async Task <object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (IsOpen)
            {
                throw new InvalidOperationException("DialogHost is already open.");
            }

            _closeDialogExecutionParameter = null; // fix to prevent dialog return the previous value when unloaded

            AssertTargetableContent();
            DialogContent = content;
            _asyncShowOpenedEventHandler  = openedEventHandler;
            _asyncShowClosingEventHandler = closingEventHandler;
            SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                _asyncShowWaitHandle.WaitOne();
            });

            task.Start();

            await task;

            _asyncShowOpenedEventHandler  = null;
            _asyncShowClosingEventHandler = null;

            return(_closeDialogExecutionParameter);
        }
Esempio n. 12
0
        private async void ShowFreshDialog(object parameter)
        {
            DialogOpenedEventHandler  openedEventHandler  = null;
            DialogClosingEventHandler closingEventHandler = null;

            Console.WriteLine("Parameter:", parameter);
            await DialogHost.Show(new FreshController(), openedEventHandler, closingEventHandler);             //TODO add CancellationToken
        }
        public static async Task <SaveFileDialogResult> ShowDialogAsync(string dialogHostName, double?width    = null, double?height   = null,
                                                                        string currentDirectory                = null, string filename = null,
                                                                        bool showHiddenFilesAndDirectories     = false, bool showSystemFilesAndDirectories      = false,
                                                                        DialogOpenedEventHandler openedHandler = null, DialogClosingEventHandler closingHandler = null)
        {
            SaveFileDialog dialog = InitDialog(width, height, currentDirectory, filename, null, -1, showHiddenFilesAndDirectories, showSystemFilesAndDirectories);

            return(await DialogHost.Show(dialog, dialogHostName, openedHandler, closingHandler) as SaveFileDialogResult);
        }
Esempio n. 14
0
        public static async Task <OpenDirectoryDialogResult> ShowDialogAsync(DialogHost dialogHost, double?width    = null, double?height = null,
                                                                             string currentDirectory                = null,
                                                                             bool showHiddenFilesAndDirectories     = false, bool showSystemFilesAndDirectories      = false,
                                                                             DialogOpenedEventHandler openedHandler = null, DialogClosingEventHandler closingHandler = null)
        {
            OpenDirectoryDialog dialog = InitDialog(width, height, currentDirectory, showHiddenFilesAndDirectories, showSystemFilesAndDirectories);

            return(await dialogHost.ShowDialog(dialog, openedHandler, closingHandler) as OpenDirectoryDialogResult);
        }
        private static async Task ShowDialogExtended(object content, object dialogIdentifier,
                                                     DialogOpenedEventHandler openedEventHandler,
                                                     DialogClosingEventHandler closingEventHandler)
        {
            var result = await DialogHost.Show(content, dialogIdentifier, openedEventHandler,
                                               closingEventHandler);

            _logger.Debug(
                $"Dialog was closed, the CommandParameter used to close it was: {result ?? "NULL"}"
                );
        }
Esempio n. 16
0
        public Task ShowAsync(object o, string dialogID, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            PassedObject = o;
            DialogID     = dialogID;
            NumberOfCalls++;

            var viewModel = o as ProgressControlViewModel;

            viewModel.OnCompleted += ViewModel_OnCompleted;
            openedEventHandler.Invoke(this, null);
            waitHandle.WaitOne();
            return(Task.CompletedTask);
        }
Esempio n. 17
0
        /// <summary>
        /// Displays a requested view.
        /// </summary>
        /// <param name="message"></param>
        public Task <object> OpenDialogAsync <TUserControl, TViewModel>
        (
            string dialogIdentifier,
            DialogOpenedEventHandler openedEventHandler,
            DialogClosingEventHandler closingEventHandler,
            TViewModel viewModel = default(TViewModel),
            object parameter     = null
        )
            where TUserControl : UserControl, new()
            where TViewModel : IViewModel, new()
        {
            var view = CreateUserControl <TUserControl, TViewModel>(viewModel, parameter);

            return(DialogHost.Show(view, dialogIdentifier, openedEventHandler, closingEventHandler));
        }
Esempio n. 18
0
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled)
            {
                return;
            }

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;

            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler  = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();

                if (_popupContentControl != null)
                {
                    switch (OpenDialogCommandDataContextSource)
                    {
                    case DialogHostOpenDialogCommandDataContextSource.SenderElement:
                        _popupContentControl.DataContext =
                            (executedRoutedEventArgs.Parameter as FrameworkElement)?.DataContext;
                        break;

                    case DialogHostOpenDialogCommandDataContextSource.DialogHostInstance:
                        _popupContentControl.DataContext = DataContext;
                        break;

                    case DialogHostOpenDialogCommandDataContextSource.None:
                        _popupContentControl.DataContext = null;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                DialogContent = executedRoutedEventArgs.Parameter;
            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
        public static async Task ShowDialogExtended(object content, object dialogIdentifier,
                                                    DialogOpenedEventHandler openedEventHandler,
                                                    DialogClosingEventHandler closingEventHandler)
        {
            content.ThrowIfNull(nameof(content));
            dialogIdentifier.ThrowIfNull(nameof(dialogIdentifier));
            openedEventHandler.ThrowIfNull(nameof(openedEventHandler));
            closingEventHandler.ThrowIfNull(nameof(closingEventHandler));

            object result = await DialogHost.Show(
                content, dialogIdentifier, openedEventHandler, closingEventHandler
                );

            _logger.Debug(
                $"Dialog was closed, the CommandParameter used to close it was: {result ?? "NULL"}."
                );
        }
Esempio n. 20
0
        private Task <object> ShowDialog(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler dialogClosingEventHandler)
        {
            HashSet <DialogHost> instances = (HashSet <DialogHost>)dialogInstancesField.GetValue(null);

            if (instances != null)
            {
                if (instances.Count == 0)
                {
                    throw new InvalidOperationException("No loaded DialogHost instances.");
                }
                DialogHost host = instances.First();
                if (host.IsOpen)
                {
                    host.IsOpen = false;
                }
                return(host.ShowDialog(content, openedEventHandler, dialogClosingEventHandler));
            }
            return(DialogHost.Show(content, openedEventHandler, dialogClosingEventHandler));
        }
Esempio n. 21
0
        public async Task ShowDialogAsync(DialogScreen dialogScreen)
        {
            // Get the view that renders this viewmodel
            var view = _viewManager.CreateAndBindViewForModelIfNecessary(dialogScreen);

            // Set up event routing that will close the view when called from viewmodel
            DialogOpenedEventHandler onDialogOpened = (sender, e) =>
            {
                // Delegate to close the dialog and unregister event handler
                void OnScreenClosed(object o, CloseEventArgs args)
                {
                    e.Session.Close();
                    dialogScreen.Closed -= OnScreenClosed;
                }

                dialogScreen.Closed += OnScreenClosed;
            };

            // Show view
            await DialogHost.Show(view, onDialogOpened);
        }
Esempio n. 22
0
        private DialogOpenedEventHandler GetDialogOpenedEventHandler(IHostDialogAware viewModel,
                                                                     IDialogParameters parameters)
        {
            if (parameters == null)
            {
                parameters = new DialogParameters();
            }

            DialogOpenedEventHandler eventHandler =
                (sender, eventArgs) =>
            {
                var _content = eventArgs.Session.Content;
                if (viewModel is IHostDialogAware aware)
                {
                    aware.OnDialogOpened(parameters);
                }
                eventArgs.Session.UpdateContent(_content);
            };

            return(eventHandler);
        }
Esempio n. 23
0
        public async Task <IDialogResult> ShowDialog(string name, IDialogParameters parameters = null, string IdentifierName = "Root")
        {
            if (parameters == null)
            {
                parameters = new DialogParameters();
            }

            var content = _containerExtension.Resolve <object>(name);

            if (!(content is FrameworkElement dialogContent))
            {
                throw new NullReferenceException("A dialog's content must be a FrameworkElement");
            }

            if (dialogContent is FrameworkElement view && view.DataContext is null && ViewModelLocator.GetAutoWireViewModel(view) is null)
            {
                ViewModelLocator.SetAutoWireViewModel(view, true);
            }

            if (!(dialogContent.DataContext is IDialogHostAware viewModel))
            {
                throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");
            }

            viewModel.IdentifierName = IdentifierName;

            DialogOpenedEventHandler eventHandler = async
                                                        (sender, eventArgs) =>
            {
                var content = eventArgs.Session.Content;
                eventArgs.Session.UpdateContent(new ProgressDialog());
                if (viewModel is IDialogHostAware aware)
                {
                    await aware.OnDialogOpenedAsync(parameters);
                }
                eventArgs.Session.UpdateContent(content);
            };

            return((IDialogResult)await DialogHost.Show(dialogContent, viewModel.IdentifierName, eventHandler));
        }
Esempio n. 24
0
        public Task <object> Show(object content, EventHandler <DialogOpenedEventArgs> openedArgs, EventHandler <DialogClosingEventArgs> closingArgs)
        {
            DialogOpenedEventHandler openedEventHandler = new DialogOpenedEventHandler((s, args) => {
                DialogOpenedEventArgs extArgs = new DialogOpenedEventArgs();
                openedArgs(s, extArgs);
                if (extArgs.ShouldClose)
                {
                    args.Session.Close();
                }
            });

            DialogClosingEventHandler dialogClosingEventHandler = new DialogClosingEventHandler((s, args) => {
                DialogClosingEventArgs extArgs = new DialogClosingEventArgs(args.Parameter);
                closingArgs(s, extArgs);
                if (extArgs.IsCancelled)
                {
                    args.Cancel();
                }
            });

            return(ShowDialog(content, openedEventHandler, dialogClosingEventHandler));
        }
Esempio n. 25
0
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled)
            {
                return;
            }

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;

            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler  = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();

                //TODO enhancement: make the following configurable, so that the data context can be pulled from the dialog host if desired.
                //      (leave the current behaviour as the default; most developers will find this logical, as the data context will "inherit" from button containing the content)

                var contentElement = executedRoutedEventArgs.Parameter as FrameworkElement;
                var senderElement  = executedRoutedEventArgs.OriginalSource as FrameworkElement;
                if (contentElement != null && senderElement != null && contentElement.DataContext == null && BindingOperations.GetBindingExpression(contentElement, DataContextProperty) == null)
                {
                    DialogContent = executedRoutedEventArgs.Parameter;
                    contentElement.SetCurrentValue(DataContextProperty, senderElement.DataContext);
                }
                else
                {
                    DialogContent = executedRoutedEventArgs.Parameter;
                }
            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
Esempio n. 26
0
 /// <summary>
 /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
 /// </summary>
 /// <param name="content">Content to show (can be a control or view model).</param>
 /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
 /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
 public static async Task <object> Show(object content, DialogOpenedEventHandler openedEventHandler)
 {
     return(await Show(content, null, openedEventHandler, null));
 }
Esempio n. 27
0
 public static void SetDialogOpenedAttached(DependencyObject element, DialogOpenedEventHandler value)
 {
     element.SetValue(DialogOpenedAttachedProperty, value);
 }
Esempio n. 28
0
        /// <summary>
        /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
        /// </summary>
        /// <param name="content">Content to show (can be a control or view model).</param>
        /// <param name="dialogIndetifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
        /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
        /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
        /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
        public static async Task <object> Show(object content, object dialogIndetifier, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (LoadedInstances.Count == 0)
            {
                throw new InvalidOperationException("No loaded DialogHost instances.");
            }
            LoadedInstances.First().Dispatcher.VerifyAccess();

            var targets = LoadedInstances.Where(dh => Equals(dh.Identifier, dialogIndetifier)).ToList();

            if (targets.Count == 0)
            {
                throw new InvalidOperationException("No loaded DialogHost matches identifier.");
            }
            if (targets.Count > 1)
            {
                throw new InvalidOperationException("Multiple viable DialogHosts.  Specify a unique identifier.");
            }
            if (targets[0].IsOpen)
            {
                throw new InvalidOperationException("DialogHost is already open.");
            }

            targets[0].AssertTargetableContent();
            targets[0].DialogContent = content;
            targets[0]._asyncShowOpenedEventHandler  = openedEventHandler;
            targets[0]._asyncShowClosingEventHandler = closingEventHandler;
            targets[0].SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                targets[0]._asyncShowWaitHandle.WaitOne();
            });

            task.Start();

            await task;

            targets[0]._asyncShowOpenedEventHandler  = null;
            targets[0]._asyncShowClosingEventHandler = null;

            return(targets[0]._closeDialogExecutionParameter);
        }
 /// <summary>
 /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
 /// </summary>
 /// <param name="content">Content to show (can be a control or view model).</param>        
 /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
 /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
 /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
 public static async Task<object> Show(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
 {
     return await Show(content, null, openedEventHandler, closingEventHandler);
 }
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled) return;

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;
            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();

                //TODO enhancement: make the following configurable, so that the data context can be pulled from the dialog host if desired.
                //      (leave the current behaviour as the default; most developers will find this logical, as the data context will "inherit" from button containing the content)

                var contentElement = executedRoutedEventArgs.Parameter as FrameworkElement;
                var senderElement = executedRoutedEventArgs.OriginalSource as FrameworkElement;
                if (contentElement != null && senderElement != null && contentElement.DataContext == null && BindingOperations.GetBindingExpression(contentElement, DataContextProperty) == null)
                {
                    DialogContent = executedRoutedEventArgs.Parameter;
                    contentElement.SetCurrentValue(DataContextProperty, senderElement.DataContext);
                }
                else
                    DialogContent = executedRoutedEventArgs.Parameter;

            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
 public static void SetDialogOpenedAttached(DependencyObject element, DialogOpenedEventHandler value)
 {
     element.SetValue(DialogOpenedAttachedProperty, value);
 }
        /// <summary>
        /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
        /// </summary>
        /// <param name="content">Content to show (can be a control or view model).</param>
        /// <param name="dialogIndetifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
        /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
        /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
        /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
        public static async Task<object> Show(object content, object dialogIndetifier, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (content == null) throw new ArgumentNullException(nameof(content));

            if (LoadedInstances.Count == 0)
                throw new InvalidOperationException("No loaded DialogHost instances.");
            LoadedInstances.First().Dispatcher.VerifyAccess();

            var targets = LoadedInstances.Where(dh => Equals(dh.Identifier, dialogIndetifier)).ToList();
            if (targets.Count == 0)
                throw new InvalidOperationException("No loaded DialogHost matches identifier.");
            if (targets.Count > 1)
                throw new InvalidOperationException("Multiple viable DialogHosts.  Specify a unique identifier.");
            if (targets[0].IsOpen)
                throw new InvalidOperationException("DialogHost is already open.");

            targets[0].AssertTargetableContent();
            targets[0].DialogContent = content;
            targets[0]._asyncShowOpenedEventHandler = openedEventHandler;
            targets[0]._asyncShowClosingEventHandler = closingEventHandler;
            targets[0].SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                targets[0]._asyncShowWaitHandle.WaitOne();
            });
            task.Start();

            await task;

            targets[0]._asyncShowOpenedEventHandler = null;
            targets[0]._asyncShowClosingEventHandler = null;

            return targets[0]._closeDialogExecutionParameter;
        }
 /// <summary>
 /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
 /// </summary>
 /// <param name="content">Content to show (can be a control or view model).</param>
 /// <param name="dialogIndetifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
 /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
 /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
 public static Task<object> Show(object content, object dialogIndetifier, DialogOpenedEventHandler openedEventHandler)
 {
     return Show(content, dialogIndetifier, openedEventHandler, null);
 }
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled) return;

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;
            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();
                DialogContent = executedRoutedEventArgs.Parameter;
            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
 /// <summary>
 /// Shows a dialog using the first found <see cref="DialogHost"/> in a given <see cref="Window"/>.
 /// </summary>
 /// <param name="window">Window on which the modal dialog should be displayed. Must contain a <see cref="DialogHost"/>.</param>
 /// <param name="content">Content to show (can be a control or view model).</param>
 /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
 /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
 /// <exception cref="InvalidOperationException">
 /// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.  
 /// </exception>
 /// <remarks>
 /// As a depth first traversal of the window's visual tree is performed, it is not safe to use this method in a situtation where a screen has multiple <see cref="DialogHost"/>s.
 /// </remarks>
 /// <returns></returns>
 public static async Task<object> ShowDialog(this Window window, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
 {
     return await GetFirstDialogHost(window).ShowInternal(content, openedEventHandler, closingEventHandler);
 }
 /// <summary>
 /// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
 /// </summary>
 /// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>        
 /// <param name="content">Content to show (can be a control or view model).</param>
 /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
 /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
 /// <exception cref="InvalidOperationException">
 /// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.  
 /// </exception>
 /// <returns></returns>
 public static async Task<object> ShowDialog(this DependencyObject childDependencyObject, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
 {
     return await GetOwningDialogHost(childDependencyObject).ShowInternal(content, openedEventHandler, closingEventHandler);
 }
        private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            if (executedRoutedEventArgs.Handled) return;

            var dependencyObject = executedRoutedEventArgs.OriginalSource as DependencyObject;
            if (dependencyObject != null)
            {
                _attachedDialogOpenedEventHandler = GetDialogOpenedAttached(dependencyObject);
                _attachedDialogClosingEventHandler = GetDialogClosingAttached(dependencyObject);
            }

            if (executedRoutedEventArgs.Parameter != null)
            {
                AssertTargetableContent();

                if (_popupContentControl != null)
                {
                    switch (OpenDialogCommandDataContextSource)
                    {
                        case DialogHostOpenDialogCommandDataContextSource.SenderElement:
                            _popupContentControl.DataContext =
                                (executedRoutedEventArgs.Parameter as FrameworkElement)?.DataContext;
                            break;
                        case DialogHostOpenDialogCommandDataContextSource.DialogHostInstance:
                            _popupContentControl.DataContext = DataContext;
                            break;
                        case DialogHostOpenDialogCommandDataContextSource.None:
                            _popupContentControl.DataContext = null;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }

                DialogContent = executedRoutedEventArgs.Parameter;
            }

            SetCurrentValue(IsOpenProperty, true);

            executedRoutedEventArgs.Handled = true;
        }
Esempio n. 38
0
 public static Task <object> PopupDialog(this UserControl con, object dialogIdentifier = null, DialogOpenedEventHandler openedEventHandler = null, DialogClosingEventHandler closingEventHandler = null) //必须为public static 类型,且参数使用this关键字
 {
     return(Application.Current.Dispatcher.Invoke <Task <object> >(() =>
     {
         return DialogHost.Show(con, dialogIdentifier, openedEventHandler, closingEventHandler);
     }));
 }
        internal async Task<object> ShowInternal(object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (IsOpen)
                throw new InvalidOperationException("DialogHost is already open.");

            AssertTargetableContent();
            DialogContent = content;
            _asyncShowOpenedEventHandler = openedEventHandler;
            _asyncShowClosingEventHandler = closingEventHandler;
            SetCurrentValue(IsOpenProperty, true);

            var task = new Task(() =>
            {
                _asyncShowWaitHandle.WaitOne();
            });
            task.Start();

            await task;

            _asyncShowOpenedEventHandler = null;
            _asyncShowClosingEventHandler = null;

            return _closeDialogExecutionParameter;
        }
Esempio n. 40
0
 public static void CloseDialog(this UserControl con, object dialogIdentifier = null, DialogOpenedEventHandler openedEventHandler = null, DialogClosingEventHandler closingEventHandler = null)    //必须为public static 类型,且参数使用this关键字
 {
     Application.Current.Dispatcher.Invoke(() =>
     {
         DialogHost.CloseDialogCommand.Execute(con, con);
         //return DialogHost.close(con, dialogIdentifier, openedEventHandler, closingEventHandler);
     });
 }
        /// <summary>
        /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
        /// </summary>
        /// <param name="content">Content to show (can be a control or view model).</param>
        /// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifer set in XAML. <c>null</c> is allowed.</param>
        /// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
        /// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
        /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
        public static async Task<object> Show(object content, object dialogIdentifier, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler)
        {
            if (content == null) throw new ArgumentNullException(nameof(content));

            if (LoadedInstances.Count == 0)
                throw new InvalidOperationException("No loaded DialogHost instances.");
            LoadedInstances.First().Dispatcher.VerifyAccess();

            var targets = LoadedInstances.Where(dh => Equals(dh.Identifier, dialogIdentifier)).ToList();
            if (targets.Count == 0)
                throw new InvalidOperationException("No loaded DialogHost have an Identifier property matching dialogIndetifier argument.");
            if (targets.Count > 1)
                throw new InvalidOperationException("Multiple viable DialogHosts.  Specify a unique Identifier on each DialogHost, especially where multiple Windows are a concern.");

            return await targets[0].ShowInternal(content, openedEventHandler, closingEventHandler);
        }