AttachView is missing

Apr 13, 2011 at 8:31 AM

here is my code:


    public class ReceiptViewModel : Screen

        public ReceiptViewModel() : base()
            var view = ViewLocator.LocateForModelType(this.GetType(), null, "Cashier");
            var view2 = ViewLocator.LocateForModelType(this.GetType(), null, "Cashier");//copy for cloned window

            base.AttachView(view, "Cashier");
            base.AttachView(view2, "Cashier2");//does not work with same content name on ActiveItem in cloned window, so I change content name to Cashier2


I start application with two windows, each has ActiveItem where I set ReceiptViewModel instance.
Both windows xaml:
<ContentControl x:Name="ActiveItem"  cal:View.Context="Cashier" />
<ContentControl x:Name="ActiveItem"  cal:View.Context="Cashier2" />
It does not work if I have both set to "Cashier" context (second window is left blank), so I thought I could create views manually and then attach them.
I succeeded when I changed Context names for the same view. It worked fine before I got latest CM updates... now AttachView method is missing ;-/


Apr 13, 2011 at 1:34 PM

As an inheritor of ViewAware, you have access to the Views cache. It's just a dictionary, so for this purpose, you could just add to it directly. The context is the key and the view is the value.

Apr 13, 2011 at 1:48 PM

Could you give me short guideline, maybe some piece of code how to access Views cache.

Apr 13, 2011 at 8:34 PM
To access the cached view, you can use something like this (in a class inheriting from Screen or simply ViewAware):
var view = this.GetView("Cashier");

The cached view, however, is only available *after* IViewAware.AttachView is called (usually from the infrastructure).

I failed to understand the reason why you are manually locating and attaching views (which is not the usual way in CM); however, to exactly
reproduce your current code, you have to use an explicit cast to access the IViewAware interface:

((IViewAware)this).AttachView(someView, "SomeContext");

since IViewAware is implemented explicitly.