How to provide DataTemplate from a prism module

I've a WPF application, with Prism, we are using some modules(and more globally, a lot of micro-services).

I'm searching for the best practices to provide to the application a template that could be used to represent a model.

Since I've to do this from a module, I cannot just create a ResourcesDictionary and add it to the App resources.

How would you do this? Our objective is to have a good isolation of features.

1 answer

  • answered 2018-04-17 06:01 Andy

    I think you've not really fully explained your purpose or what you have here. With that proviso in mind.

    If you created a regular resource dictionary in an app you can merge that in app.xaml.

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    And you can then target resources in there in markup using x:Key or implicit styling. And templating which targets a datatype.

    What that does is add the things in your resource dictionary to a sort of dictionary. ( It's not exactly a dictionary ) It has key and value.
    That has scope of your entire application.
    You can reference the things in there and switch them out. You can merge another resource dictionary in code. Once they're in there though, they are there to stay until you shut the app down or clear those resources.

    You can read a resource dictionary:

    ResourceDictionary rd = new ResourceDictionary
    {
         Source = new Uri("Dictionary1.xaml", UriKind.Relative)
    };
    

    And merge it:

    Application.Current.Resources.MergedDictionaries.Add(rd);
    

    If you merge a resource dictionary in markup, it doesn't even have to be compiled. I never tried that in code but I guess you might well find you could merge a "loose" uncompiled resource dictionary. If it didn't work directly you could definitely xamlreader.Load or .Parse an uncompiled rd into one in memory.

    That adds to application scope. If you wanted that then maybe you ought to just merge your resource dictionaries in app.xaml though.
    If you want scope, then windows, usercontrols etc etc all have resources. You can merge a resource dictionary in at pretty much any level you fancy and it'll then have a narrower scope than application.