Azzarrel
Azzarrel

Reputation: 573

Opening new window in a MVVM WPF Class Libary

Basically I am looking for this Answer, but for Class Libraries.

I am currently maintaining a larger WPF Solution where each part is loaded as a Add-In. I'd like to create a service for all of these Add-Ins to implement which opens a Window without using UI elements in the ViewModel, basically looking like this:

class WindowService:IWindowService
{
    public void ShowWindow(object viewModel)
    {
        var win = new Window();
        win.Content = viewModel;
        win.Show();
    }
} 

To simplify it, let's say we have 4 Projects:

1. Client


2. Add-Ins

The Answer I linked defines DataTemplates in the App.Xaml, but I can't define DataTemplates for Classes I don't know at that point, since Add-Ins are loaded using Reflection at Runtime.

Where would I put my DataTemplates in order to get the linked Answer to work for Class Libraries without an App.Xaml?

Upvotes: 0

Views: 96

Answers (1)

Andrew Stephens
Andrew Stephens

Reputation: 10193

What I do is have a xaml resource file in each of my class libraries, and programatically merge these into the main application resources.

Say the class library project assembly name is "MyProject.Client" and it contains the resource file \Views\Resources\LibraryStyles.xaml. The code to merge this would look like:

Application.Current.Resources.MergedDictionaries.Add(
    new ResourceDictionary
        {
            Source = new Uri("pack://application:,,,/MyProject.Client;component/Views/Resources/LibraryStyles.xaml",
            UriKind.RelativeOrAbsolute)
        });

Upvotes: 1

Related Questions