/// <summary>
        /// Creates a <see cref="ContentDialog" /> of specified type using
        /// <see cref="Activator.CreateInstance(Type)"/>.
        /// </summary>
        public IContentDialog Create(Type dialogType)
        {
            if (dialogType == null)
            {
                throw new ArgumentNullException(nameof(dialogType));
            }

            var instance = Activator.CreateInstance(dialogType);

            // Is instance of type IContentDialog?
            IContentDialog customContentDialog = instance as IContentDialog;

            if (customContentDialog != null)
            {
                return(customContentDialog);
            }

            // Is instance of type ContentDialog?
            var contentDialog = instance as ContentDialog;

            if (contentDialog != null)
            {
                return(new ContentDialogWrapper(contentDialog));
            }

            throw new ArgumentException($"Only dialogs of type {typeof(ContentDialog)} or {typeof(IContentDialog)} are supported.");
        }
예제 #2
0
        void ShowDialogInternal(string name, IDialogParameters parameters, Action <IDialogResult> callback, string windowName = null)
        {
            IContentDialog contentDialog = CreateDialogWindow(windowName);

            ConfigureDialogWindowEvents(contentDialog, callback);
            ConfigureDialogWindowContent(name, contentDialog, parameters);

            _ = contentDialog.ShowAsync();
        }
예제 #3
0
        private IAsyncOperation <ContentDialogResult> ShowContentDialogAsync(
            INotifyPropertyChanged viewModel,
            Type contentDialogType)
        {
            Logger.Write($"Content dialog: {contentDialogType}; View model: {viewModel.GetType()}");

            IContentDialog dialog = CreateContentDialog(contentDialogType, viewModel);

            return(dialog.ShowAsync());
        }
예제 #4
0
        void ConfigureDialogWindowProperties(IContentDialog window, FrameworkElement dialogContent, IDialogAware viewModel)
        {
            var windowStyle = Dialog.GetWindowStyle(dialogContent);

            if (windowStyle != null)
            {
                window.Style = windowStyle;
            }

            window.Content     = dialogContent;
            window.DataContext = viewModel;
        }
예제 #5
0
        void ConfigureDialogWindowContent(string dialogName, IContentDialog window, IDialogParameters parameters)
        {
            var content       = _containerExtension.Resolve <object>(dialogName);
            var dialogContent = content as FrameworkElement;

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

            var viewModel = dialogContent.DataContext as IDialogAware;

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

            ConfigureDialogWindowProperties(window, dialogContent, viewModel);

            MvvmHelpers.ViewAndViewModelAction <IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
        }
예제 #6
0
        void ConfigureDialogWindowEvents(IContentDialog contentDialog, Action <IDialogResult> callback)
        {
            IDialogResult result = null;

            Action <IDialogResult> requestCloseHandler = null;

            requestCloseHandler = (o) =>
            {
                result = o;
                contentDialog.Hide();
            };

            RoutedEventHandler loadedHandler = null;

            loadedHandler = (o, e) =>
            {
                contentDialog.Loaded -= loadedHandler;

                if (contentDialog.DataContext is IDialogAware dialogAware)
                {
                    dialogAware.RequestClose += requestCloseHandler;
                }
            };

            contentDialog.Loaded += loadedHandler;

            TypedEventHandler <ContentDialog, ContentDialogClosingEventArgs> closingHandler = null;

            closingHandler = (o, e) =>
            {
                if (contentDialog.DataContext is IDialogAware dialogAware &&
                    !dialogAware.CanCloseDialog())
                {
                    e.Cancel = true;
                }
            };

            contentDialog.Closing += closingHandler;

            TypedEventHandler <ContentDialog, ContentDialogClosedEventArgs> closedHandler = null;

            closedHandler = (o, e) =>
            {
                contentDialog.Closed  -= closedHandler;
                contentDialog.Closing -= closingHandler;

                if (contentDialog.DataContext is IDialogAware dialogAware)
                {
                    dialogAware.RequestClose -= requestCloseHandler;

                    dialogAware.OnDialogClosed();
                }

                if (result == null)
                {
                    result = new DialogResult();
                }

                callback?.Invoke(result);

                contentDialog.DataContext = null;
                contentDialog.Content     = null;
            };
            contentDialog.Closed += closedHandler;
        }