We have a suite of .net MAUI apps which all use the same back end services, data models, etc.
We use Visual Studio and are looking for how to separate all of the shared code, put it in a separate project, and then somehow attach that to each app Solution may be as a DLL or something.
Many thanks for any help you can offer!
What you need to create is a .NET MAUI Class Library
. You can find relevant project template available in Visual Studio.
Within this class library, include all the shared code, such as backend service implementations, data models, and any other common functionality.
Now In each .NET MAUI application that needs to utilize the shared code, add a reference to the class library:
- Right-click on the MAUI project in Solution Explorer.
- Select “Add” > “Project Reference.”
- Choose the class library project and click “OK.”
I agree with the previous answer, and would like to suggest the additional step of packaging your class libraries for “back end services, data models, etc.” as NuGet. The easiest way that I know of to do this is setting <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
in the csproj
file for your class library. Then, once you build the project you should be able to locate the package in your bin
directory.
You’ll probably want this package to be visible e.g. in the Visual Studio NuGet package Manager. There isn’t anything special about a package “server”, it just needs to be one of the Package Source:
locations you’ve set up in the NuGet manager, and it could be:
- A local folder on the machine
- A company-wide folder on the network.
- https://nuget.org (it’s a good idea to reserve your company’s
prefix) - The more integration-oriented https://int.nugettest.org/ during development.
An example of a .csproj
file configured for nuget.org
can be found here.