This project has moved. For the latest updates, please go here.

When is View instantiated

Feb 17, 2012 at 2:57 AM
Edited Feb 17, 2012 at 3:00 AM

I request a xap using this command:

        public void LoadCatalogs()
        {
            Deployment.RequestXap("DefaultParts.xap", AfterDefaultXapLoaded);
        }

        public void AfterDefaultXapLoaded(Exception ex)
        {
            ...
        }

 Included in that xap is a view that I've defined like this:

    [ExportAsView("Enterprise")]
    public partial class EnterpriseView : UserControl

If I put breakpoints in the 'AfterDefaultXapLoaded' method and in the constructor for the Enterprise view, the breakpoint for the Enterprise view will hit first.  So, the Enterprise view is being instantiated when the xap is downloaded.

This contradicts my understanding.  I thought that, since Jounce imports views using the Lazy<> technique, the view would not be instantiated until it was actually published. 

Can anyone explain why the view is being instantiated at download?

Thanks.

 

Coordinator
Feb 17, 2012 at 12:40 PM

There could be any number of reasons - for example, if you have an import on views somewhere that isn't using Lazy (remember that they can be imported multiple times, in multiple places) - I'd have to dig into a more concrete example, but since you aren't routing the view to a region I'm not sure why it would get instantiated at all.

Feb 17, 2012 at 2:54 PM

Thanks for the prompt response.  That's helpful.  You confirmed my understanding that, barring exceptions, a lazy view should not be instantiated at download.  Below is the entire view, both xaml and code-behind ('usings' deleted).  Do you see anything that would trigger an instantiation?  (Just to repeat what I said earlier, if I put a breakpoint at the InitializeComponent line below, and one in the AfterDefaultXapLoaded (from above), the InitializeComponent breakpoint is hit first).  Thanks.

<UserControl
    x:Class="Illuminaction.Views.EnterpriseView" 
    x:Name="EnterpriseViewControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:regions="clr-namespace:Jounce.Regions;assembly=Jounce"
    xmlns:views="clr-namespace:Illuminaction.Views" 
    mc:Ignorable="d" d:DesignWidth="1000" d:DesignHeight="700">

    <UserControl.Resources>
        <LinearGradientBrush x:Key="YellowGradientBrush" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Yellow"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" IsHitTestVisible="{Binding IsHitTestVisible}" Background="{StaticResource YellowGradientBrush}">
        <!--<ContentControl Name="EnterpriseContentControl" regions:ExportAsRegion.RegionName="EnterpriseContentRegion" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>-->
        <ContentControl Name="EnterpriseContentControl" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
    </Grid>
</UserControl>

 

    [ExportAsView("Enterprise")]
    public partial class EnterpriseView : UserControl
    {
        public EnterpriseView()
        {
            InitializeComponent();
        }


        [Export]
        public ViewModelRoute Binding
        {
            get
            {
                return ViewModelRoute.Create("Enterprise", "Enterprise");
            }
        }

        MainViewModel MVM 
        {
            get { return ((EnterpriseViewModel)LayoutRoot.DataContext).MVM; }
        }

    }

 

 

Feb 17, 2012 at 4:38 PM
Edited Feb 17, 2012 at 5:29 PM
reswat wrote:
        [Export]
        public ViewModelRoute Binding
        {
            get
            {
                return ViewModelRoute.Create("Enterprise", "Enterprise");
            }
        }

This is the culprit. Jounce's MEF infrastructure has to instantiate EnterpriseView to get at the ViewModelRoute [Export].

If you want to avoid instantiation of a View the way to go is via FluentViewModelRouter - just declare a class derived from IModuleInitializer with an [Import] for IFluentViewModelRouter and perform the required routing configuration in its Initialize()-Method.

Feb 17, 2012 at 4:55 PM

Cool.  Thanks.

I'm not sure which way I'll go.  It's not terrible that this is getting instantiated, so I may leave it.  But it's nice to know what was causing it and it's nice to know how to fix it.