Exception after active view - Windows 8

Topics: Prism v4 - WPF 4
Apr 22, 2013 at 1:44 PM
Edited Apr 22, 2013 at 4:30 PM

I'm working for long time on a project with WPF and Prism 4, after I upgrade my OS to Win 8 I have got an exception when I Activate the second view, the same code run on Win 7 without a problem.
I upgrade the prism to 4.1 but the exception still occur
finally I debug it with the source of Prism and what I have found is that:
In the the activation process, the new view deactivate the old view, and what is happened is
this.hostControl.SelectedItem = null (in SelectorItemsSourceSyncBehavior.cs)
and later GetItemMetadataOrThrow(view) (in Regions.cs) throw excpetion of null.

If I comment the the line this.Region.Deactivate(this.hostControl.SelectedItem);
in function ActiveViews_CollectionChanged() (in SelectorItemsSourceSyncBehavior.cs)
there is no exception

Ant ideas ?



My code is
if (view != null) 
    lock (myLock)    // myLock is just a static object
        if (view != null) 
            theRegion = (Region)s;
Prism code

The function that throw exception

 public virtual void Activate(object view)
            ItemMetadata itemMetadata = this.GetItemMetadataOrThrow(view);

            if (!itemMetadata.IsActive)
                itemMetadata.IsActive = true;
The function that change the select item to null
 private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            if (this.updatingActiveViewsInHostControlSelectionChanged)
                // If we are updating the ActiveViews collection in the HostControlSelectionChanged, that 
                // means the user has set the SelectedItem or SelectedItems himself and we don't need to do that here now

            if (e.Action == NotifyCollectionChangedAction.Add)
                if (this.hostControl.SelectedItem != null
                    && this.hostControl.SelectedItem != e.NewItems[0]
                    && this.Region.ActiveViews.Contains(this.hostControl.SelectedItem))
                    __// make exception in windows 8 
                   this.Region.Deactivate(this.hostControl.SelectedItem);  __

                this.hostControl.SelectedItem = e.NewItems[0];
            else if (e.Action == NotifyCollectionChangedAction.Remove &&
                this.hostControl.SelectedItem = null;
Apr 22, 2013 at 7:04 PM

It is strange that this section of the Prism library no longer works for you in Windows 8. Also, I believe we haven't received any similar issues before. Therefore, it would be helpful if you can provide more information about the context of the application, like where and when you are executing this code so we can look at this in further detail.

Also, we noticed that you are a using a lock statement, so we assume that your code is being called by different threads. If that is the case, it would be helpful to know if those are UI threads, background threads, etc. as Prism work not work correctly in every kind of thread.


Federico Martinez
Apr 23, 2013 at 8:34 AM
Hi Federico,

The application is kiosk, and I run the the code after user touch the screen, and I want to show him the next view.
In the shell I have the main region, when I load the modules each module add his view to the main region,
I show the first view (work fine) and after that when I try to activate a different view I got the exception.
The lock statement is additional code trying to solve the problem, the exception occur with and without the lock statement.

The Thread is the UI thread.


Yair Tobi
Apr 23, 2013 at 7:10 PM
Hi Yair,

I haven't been able to reproduce this issue either by using the Prism QuickStarts or other samples. Hence, I believe there could be another component in your application that might be firing this problem.

As a quick test, please try to deactivate the current active view just right before activating the second one and check if this issue keeps appearing.

If this does not help, it would be useful to know if there is any custom behavior or logic that might be affecting the views, like for example, if the views are being reordered automatically inside the region using a SortComparison, etc. If this is not the case, then it would be helpful if you could provide us with a repro sample application so that we can analyze this problem in further detail.


Damian Cherubini
Apr 25, 2013 at 4:20 PM
Hi Damian,

OK I found the problem, when I initialize the application I add all my views to the main region, one of the views was null.
When I activated a view which is not null the exception occur.
The strange issue is that on Windows 7 or XP there was no exception.

Thanks a lot