Creating a Shared Project in WPF

The new thing for VS 2013 is Universal Apps. They allow you to create a Win 8 and Phone app with shared code. But what if you’re writing LOB apps in WPF, maybe using MVVM? You may have a Winforms app and a XAML app, and want to share code; or have two WPF apps that share a core code base, and some screens.

This post walks through creating a sample shared app.

Create WPF App

I’ll just cover WPF in this post. Start by creating a new application:

sharedcode1

Okay, so we have a WPF application (as you might already have). How to create a shared project? Well, the bad news is: you can’t; at least as far as I’m aware (please, please, please let me know in the comments if you know this to be false!).

So, if you can’t create a shared application without a universal one then create a universal one in the same solution:

sharedcode2

This creates a shared app with a reference from Windows 8.1 and Windows Phone 8.1:

sharedcode3

Now, what comes next might feel like a hackā€¦ and there’s a good reason for that!

Have a look in the Win 8 app csproj:

sharedcode4

Copy this into the csproj file of the WPF project (remember the path may be different – although in this case, are not):

sharedcode6

The important thing here is the `Label`. However, you’re most likely to get the path wrong. If you do then when you update the project in VS it will fail to load and give you an error message telling you where it’s trying to find the shared project. Having done this a couple of times, my advice is have this error message up, and try to navigate to the path in Windows Explorer: you should see where you went wrong quickly enough.

Adding stuff to the shared project

Start by adding a page to the shared project so that we can see that it works:

sharedcode7

Make it obvious so that you can recognise it:

sharedcode8

Okay, so you’ve now created a shared project, added some shared content, and referenced it from your main project. Next, tidy up by removing Windows 8 and Phone 8 apps and add a second WPF app:

sharedcode9

To make sure that this is, in fact, a shared project, create and make the same change to the second project and then modify each to call the shared page; e.g.:

    private void Button_Click(object sender, RoutedEventArgs e)
        {
            TestPage test = new TestPage();
            Window w = new Window();
            w.Content = test;
            w.Show();

        }

Then run both projects:

sharedcode10

Notes, and using this in an MVVM architecture

Okay – some things that are worth noting here. The main thing is this: the shared code is just that – shared. If you call something like:

typeof(TestPage).Assembly

You will get the assembly of the running application. Effectively, you’ve created a kind of link between the projects, without the messiness of actually creating a link. The consequences of this are that you must ensure that code in the shared project only has dependencies that can be resolved in ALL the projects that include the shared project.

MVVMCross

I’ve tried this on MVVM Cross using two projects and sharing XAML files; and it works. MVVM Cross looks in the current assembly for a `MyView`, when you navigate to a `MyViewModel`. It will find it providing all your projects reference the shared project and you do not try to re-implement the view.

To clarify:

– Creating a shared project between Winforms and WPF that shares a XAML file will most probably not work (admittedly I haven’t tried it).

– Creating a shared project for two WPF projects and copying MyView.xaml into the shared project, leaving the original intact will cause MVVM Cross to fail to resolve the page (because it doesn’t know which one you want).

5 thoughts on “Creating a Shared Project in WPF

  1. Henrique Clausing

    Thanks for these tutorial. Very good. Congratulations.

    Have you ever tried this on Visual Studio 2015 RC, using universal apps for Windows 10 and WPF?

    On WPF sides, it compiles fine, but the Windows 10 Universal Application didn’t compile, and gives the folowing error:

    Error: Length cannot be less than zero.
    Parameter name: length

    Reply
      1. Henrique Clausing

        Ok thanks.

        Actually I’m not under Windows 10 O.S. My O.S. is Windows 8.1.

        Explaining better: I have Two Projects:
        01) WPF
        02) Windows Universal App (for Windows 10 Devices Family)
        + A Shared Project, following yout nice tutorial.

        The first ONE, compiles well, even referencing the shared Project.

        The UniversalApp Project, only compiles if I don’t reference the shared Project. If I try to reference it, the compiler returns the error mencioned on my first comment.

        Regards.

        Reply
        1. pcmichaels Post author

          Strange one – are you referencing any third party tools? I’m assuming that everything in post .Net 4.5?

          Reply
          1. Henrique Clausing

            Hi!

            No. Any third party tools. It’s just a sample Project, created on this order:

            SharedProject
            WPFApplicationProject
            UniversalAppProject

            Maybe a VS2015RC bug?

            Thanks for attention.

            Regards.

Leave a Reply

Your email address will not be published. Required fields are marked *