Using the DialogConductor from the HelloScreens sample in a WPF app

Topics: Getting Started, UI Architecture
Oct 24, 2011 at 6:24 PM

Hello,

 

I have a couple of questions about the dialogconductor which is used in the HelloScreens sample.

I am using this code in a WPF application, but when I call the ShowDialog function no dialog is shown.

Could someone explain why this dialog is not shown when used in WPF?

I am still learning to work with the Caliburn Micro framework and was wondering why this doesn’t work.

 

I have now resolved this with a WindowManager (see below), is this a good approach for showing a dialog in a WPF application? Or should I rewrite the whole class when using it in a WPF application?  

 

    [Export(typeof(IDialogManager)), PartCreationPolicy(CreationPolicy.NonShared)]
    public class DialogConductorViewModel : PropertyChangedBase, IDialogManager, IConductActiveItem {
        readonly Func<IMessageBox> createMessageBox;

        [ImportingConstructor]
        public DialogConductorViewModel(Func<IMessageBox> messageBoxFactory) {
            createMessageBox = messageBoxFactory;
        }

        public IScreen ActiveItem { get; private set; }

        //*****Added******
        [Import]
        public IWindowManager WindowManager;
        //****************

        public IEnumerable GetChildren() {
            return ActiveItem != null ? new[] { ActiveItem } : new object[0];
        }

        public void ActivateItem(object item) {
            ActiveItem = item as IScreen;

            var child = ActiveItem as IChild;
            if(child != null)
                child.Parent = this;

            if (ActiveItem != null)
            {
                //*****Added******   
                WindowManager.ShowDialog(this);
                //****************
                //Removed
                //ActiveItem.Activate();
            }
            NotifyOfPropertyChange(() => ActiveItem);
            ActivationProcessed(this, new ActivationProcessedEventArgs { Item = ActiveItem, Success = true });
        }

        public void DeactivateItem(object item, bool close) {
            var guard = item as IGuardClose;
            if(guard != null) {
                guard.CanClose(result => {
                    if(result)
                        CloseActiveItemCore();
                });
            }
            else CloseActiveItemCore();
        }

        object IHaveActiveItem.ActiveItem
        {
            get { return ActiveItem; }
            set { ActivateItem(value); }
        }

        public event EventHandler<ActivationProcessedEventArgs> ActivationProcessed = delegate { };

        public void ShowDialog(IScreen dialogModel) {
            ActivateItem(dialogModel);
        }

        public void ShowMessageBox(string message, string title = "Hello Screens", MessageBoxOptions options = MessageBoxOptions.Ok, Action<IMessageBox> callback = null) {
            var box = createMessageBox();

            box.DisplayName = title;
            box.Options = options;
            box.Message = message;

            if(callback != null)
                box.Deactivated += delegate { callback(box); };

            ActivateItem(box);
        }

        void CloseActiveItemCore() {
            var oldItem = ActiveItem;
            ActivateItem(null);
            oldItem.Deactivate(true);
        }
    }

 

Thanks in advance,

 

Marcel