Hardest problem i ever had with CM.

Feb 15, 2013 at 9:57 AM
Here is a conductor i made. Everything works fine except that when i have two dialogs ,when one closes with TryClose the other one cant be closed . I do everything but it gets added to Items list

PaymentDiscountViewModel.TryClose();
d.DeactivateItem(PaymentDiscountViewModel,true);
d.CloseItem(PaymentDiscountViewModel);


d.Items.Clear but still doesnt work!

The only fix is to return null at DetermineNextItemToActivate. Then list stays empty



{
using System;
using System.Collections;
using System.ComponentModel.Composition;
using Framework;
using Caliburn.Micro;

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

    [ImportingConstructor]
    public DialogConductorViewModel(Func<IMessageBox> messageBoxFactory)
    {

        createMessageBox = messageBoxFactory;
        LayerVisibility = System.Windows.Visibility.Collapsed;
    }

    private System.Windows.Visibility layerVisibility;

    public System.Windows.Visibility LayerVisibility
    {
        get {


            return layerVisibility;
        }
        set
        {

        layerVisibility = value;
        NotifyOfPropertyChange(() => LayerVisibility);

    } }


   // public IScreen ActiveItem { get; set; }





    public void ActivateItem(object item)
    {

        if (item == null ) return;
        if (!this.Items.Contains(item as IScreen))
        {
            this.Items.Add(item as IScreen);
        }                       

        base.ActivateItem(item as IScreen);

        LayerVisibility = System.Windows.Visibility.Visible;


    }

    public void DeactivateItem(object item, bool close)
    {
    //    if (item == null) return;
        if (this.Items.Contains(item as IScreen))
        {
            this.Items.Remove(item as IScreen);
        }

        base.DeactivateItem(item as IScreen, true);




    }


    protected override IScreen DetermineNextItemToActivate(System.Collections.Generic.IList<IScreen> list, int lastIndex)
    {

     //   return null;


        if (lastIndex - 1 >= 0)
        {
            return list[lastIndex - 1];
        }
        else
        {
            LayerVisibility = System.Windows.Visibility.Collapsed;
            return null;
        }



    }



    protected override void OnDeactivate(bool close)
    {
        base.OnDeactivate(close);
    }


    public void ShowDialog(IScreen dialogModel, Action<object> callback = null)
    {
        ActivateItem(dialogModel);

        if (callback != null)
            dialogModel.Deactivated += delegate { callback(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);
    }


}