CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

MEF and Design Time in Silverlight with no App.cs

404 Views
Copy Code Show/Hide Line Numbers
    public class ViewModelLocator {
        public ViewModelLocator() {
            //if (!ViewModelBase.IsInDesignModeStatic)
            CompositionInitializer.SatisfyImports(this);
        }
 
                //If I do not use AllowDefault = true, the Design Time blows up saying it cannot
                //  match the export to the import
        [Import(AllowDefault = true)]
        public OrbitScreenViewModel SomeViewModelInstance { get; set; }
 
        [Import(AllowDefault = true)]
        public LeaseViewModel LeaseViewModel { get; set; }
 
        }
 
    [Export]
    public class OrbitScreenViewModel : ViewModelBase {
        public OrbitScreenViewModel() {
            if (IsInDesignMode) {
                // Code runs in Blend --> create design time data.
            }
            else { //Do runtime things
 
                        }
                 }
 
        }
 
//There is no App.cs or App.xaml because this is a dynamically loaded xap package for App Partioning
<Navigation:DynamicPage xmlns:vm="clr-namespace:DataForms.Locator" x:Class="DataForms.Screen"
        d:DesignWidth="800" d:DesignHeight="700"
        NavigationCacheMode="Enabled"
        Title="Data">
    <Navigation:DynamicPage.Resources>
            <vm:ViewModelLocator x:Key="VMLocator"/>
    </Navigation:DynamicPage.Resources>
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource VMLocator}, Path=SomeViewModelInstance }">
      ...
      <local:SomeUserControl DataContext="{Binding Source={StaticResource VMLocator}, Path=LeaseViewModel }"/ >    </Grid>
by CodeFuzion
  March 20, 2010 @ 11:11am
Description:
I am trying to get MEF to load in my ViewModels and use the Service Locator pattern to be able to design it. It works fine in runtime, but at design time it will not work. I can get it to work if I do AllowDefault = true on the import, because it cannot find the exports to match it. Is there any reason MEF cannot find the Exports in a Silverlight Application that has no App.cs?

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate