/// <summary> /// Constructor /// </summary> /// <param name="displayLocation"> The <see cref="DialogDisplayLocation"/> of this <see cref="DialogHandler"/>. </param> /// <param name="dialogViewProvider"> <see cref="DialogAssemblyViewProvider"/> used to obtain the proper view for an <see cref="IDialogContainerViewModel"/>. </param> public DialogHandler ( DialogDisplayLocation displayLocation, DialogAssemblyViewProvider dialogViewProvider ) { // Save parameters. this.DisplayLocation = displayLocation; _containerViewModel = new DialogContainerViewModel(); // Initialize fields. _containerView = dialogViewProvider.GetViewInstance(_containerViewModel); _dialogs = new ConcurrentStack <Dialog>(); this.IsInitialized = false; }
/// <summary> /// Initializes the current instance once the original framework <paramref name="element"/> has been loaded. /// </summary> /// <param name="element"> The <see cref="Visual"/> that will be used to display a dialog. </param> /// <param name="dialogContainerView"> The view for the <paramref name="dialogContainerViewModel"/>. </param> /// <param name="dialogContainerViewModel"> The <see cref="IDialogContainerViewModel"/> view model. </param> private void Initialize(Visual element, Visual dialogContainerView, IDialogContainerViewModel dialogContainerViewModel) { // Get the adorner layer. _adornerLayer = DialogHandler.GetAdornerLayer(element, out var adornedVisual); // Check if the really adorned visual is of a UIElement. if (!(adornedVisual is UIElement adornedElement)) { throw new DialogException($"The adorned element '{adornedVisual}' is not of type '{nameof(UIElement)}'"); } // Save the adorned element. _adornedElement = adornedElement; // Create a content adorner. _contentAdorner = new ContentAdorner(adornedElement, dialogContainerView); // Modify the dialog container view. if (dialogContainerView is FrameworkElement dialogContainerElement) { // Make the dialog container view focusable, so that key events will be received. dialogContainerElement.Focusable = true; // Handle the loaded event of the dialog container view. void LoadedHandler(object sender, RoutedEventArgs args) { dialogContainerElement.Loaded -= LoadedHandler; // Focus the dialog container view once it is loaded. dialogContainerElement.Focus(); // Attach key up events from the view to the handler in the view model. dialogContainerElement.PreviewKeyUp -= dialogContainerViewModel.HandleKeyUp; dialogContainerElement.PreviewKeyUp += dialogContainerViewModel.HandleKeyUp; } dialogContainerElement.Loaded += LoadedHandler; if (dialogContainerElement.IsLoaded) { LoadedHandler(null, new RoutedEventArgs()); } } this.OnInitialized(); }