Import/Export ResourceDictionary

Jun 17, 2010 at 12:50 PM

hi, is there any simple way of import/export ResourceDictionary via MefedMVVM into App.xaml.cs? Something like:

public partial class App : Application, IPartImportsSatisfiedNotification
    {
        [ImportMany("Resourcen", typeof(ResourceDictionary))]
        private IEnumerable<ResourceDictionary> ImportResourcen { get; set; }

        #region Implementation of IPartImportsSatisfiedNotification

        public void OnImportsSatisfied()
        {
            Debug.WriteLine("OnImportsSatisfied App.xaml");

            foreach (var dic in ImportResourcen)
            {
                this.Resources.MergedDictionaries.Add(dic);
            }
        }

        #endregion
    }
if its possible, i could import the DataTemplates for my exported ViewModels. 
i really dont know how to export a ResourceDictionary...
thx frank

 

 

 

 
Jun 17, 2010 at 3:52 PM

Have a look at this:

http://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handling

Jun 18, 2010 at 5:37 AM

i tried this, but the OnImportsSatisfied() in my app.xaml.cs never get called.

Jun 18, 2010 at 6:46 AM

I'm not sure but try calling the CompositionInitializer.SatisfyImport(this) like this:

 

void App_Startup(object sender, StartupEventArgs e)      

{

            CompositionInitializer.SatisfyImports(this);

}

Jun 18, 2010 at 7:49 AM

thx a lot:) this was the missing piece

protected override void OnStartup(StartupEventArgs e)
{
            base.OnStartup(e);
            
            ViewModelRepository.Instance.Resolver.Container.SatisfyImportsOnce(this);
}
Coordinator
Jun 18, 2010 at 7:57 AM

I would not suggest doing such a thing... I think the Resource Dictionaries should be merged manually in the App.xaml. Is there any reason you want to do it dynamically?

Jun 18, 2010 at 8:09 AM

i have extern librarys where i import viewmodels for my main app.  in my main app the user can pick the extern service and it will be presented via contentpresenter. so i need a datatemplate for my extern viewmodel and thats the point why i need to import the resourcedictionary from the extern dll as well and merge it into my main app resourcedictionary. all resourcedictionarys from referenced librarys i merge in the app.xaml XAML of course.

do you have another way to get my datatemplates without referencing the extern.dll?

Coordinator
Jun 18, 2010 at 8:38 AM
do you have another way to get my datatemplates without referencing the extern.dll?
>> No, so I guess what you are doing is correct.

Yet having said this, are you sure you need to merge the ResourceDictionaries in the App.xaml. What if only your Views in your dlls use the corresponding ResourceDictionaries...


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Fri, Jun 18, 2010 at 9:10 AM, blindmeis <notifications@codeplex.com> wrote:

From: blindmeis

i have extern librarys where i import viewmodels for my main app.  in my main app the user can pick the extern service and it will be presented via contentpresenter. so i need a datatemplate for my extern viewmodel and thats the point why i need to import the resourcedictionary from the extern dll as well and merge it into my main app resourcedictionary. all resourcedictionarys from referenced librarys i merge in the app.xaml XAML of course.

do you have another way to get my datatemplates without referencing the extern.dll?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jun 18, 2010 at 9:11 AM
Edited Jun 18, 2010 at 9:23 AM

Yet having said this, are you sure you need to merge the ResourceDictionaries in the App.xaml. What if only your Views in your dlls use the corresponding ResourceDictionaries...
>> due my ViewModel First approach the "last" view (in my main app of course) which need the datatemplate its the one with the contentpresenter. i tried this and its works too. but cause of  ViewModel First the View its always instantiate. so Import and OnImportSatisfied() also called every time. so i dont know whats better. one time in app.xaml.cs or every time in view.cs?

What if only your Views in your dlls use the corresponding ResourceDictionaries
>> do you mean my UserControls in the extern dll where i import the ViewModels from? if yes, i dont know how it should work^^

thx frank

my poor english.. sry

Coordinator
Jun 18, 2010 at 9:22 AM
I didn't know you have ViewModel first approach taht is why I was suugesting another way...

Since you have ViewModel first (i.e DataContextAware viewModels) I think you are doing the right thing.

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Fri, Jun 18, 2010 at 10:11 AM, blindmeis <notifications@codeplex.com> wrote:

From: blindmeis

Yet having said this, are you sure you need to merge the ResourceDictionaries in the App.xaml. What if only your Views in your dlls use the corresponding ResourceDictionaries...
>> due my ViewModel First approach the "last" view (in my main app of course) which need the datatemplate its the one with the contentpresenter. i tried this and its works too. so i will go importing resourcedictionarys in my views.

What if only your Views in your dlls use the corresponding ResourceDictionaries
>> do you mean my UserControls in the extern dll where i import the ViewModels from? if yes, i dont know how it should work^^

thx frank

my poor english.. sry

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com