Hybrid example, ActivateItem

Jan 31, 2011 at 8:48 PM

In the Hybrid example DocumentWorkspace class there is a Show method which calls Parent.ActivateItem. Before Parent seems to be of type IConductor in the Screen class but it's now of type object. What should Show call instead of Parent? Or maybe there is an updated sample?

DocumentWorkspace:

public void Show() {
  if(Parent.ActiveItem == this) {
    DisplayName = IconName;
    State = DocumentWorkspaceState.Master;
  }
  else
    Parent.ActivateItem(this);
}

Coordinator
Jan 31, 2011 at 8:53 PM

All the samples are now under source. Have a look there. Some changes have been made.

Jan 31, 2011 at 10:47 PM

Ever since the refactor of Parent as an object I've added the following to my screen base class:

 

public bool TryActivateItem(object item)
        {
            if (Parent == null) return false;

            if (item == null) return false;

            var iConductActiveItem = Parent as IConductActiveItem;

            if (iConductActiveItem == null) return false;

            iConductActiveItem.ActivateItem((item));

            return true;
        }

Jan 31, 2011 at 11:24 PM

Hi Joe 

Is it ok to do that ? ... won't it add the item to the parent's Items ? or is that what you're intending it to do 

 

Sorry if that seems like a daft question - I'm half asleep ;-)

Stu

 

Feb 1, 2011 at 3:56 AM

Nope, ActivateItem isn't doing any instance resolve/creation is takes an existing instance of an object to activate on a conductor. My code is a bit tangental to the original question though. All my code does is attempt to caste the parent as something that conducts an active item (Implements IConductActiveItem) and activates it through the Parent, if Parent is null or can't be caste then my method returns false. It's a safe way to try to activate without throwing on failure (though some people may want to throw). Rob's new code in the Hello Screens examples uses a property getter that castes the Parent:

protected IConductor Conductor { get { return (IConductor)Parent; } }

Which will throw if Parent is null or isn't casteable as a IConductor (which may be what you want).