I have a Win UI3 project which declares a generic host in the main app which is used for dependency injection, etc. The main application will support many different document types, to keep it more manageable I'd like to separate the code and UI (page XAMLs and View Models) for each document type into a separate libraries.
Within the main app the pages are instantiated by the Frame.Navigate method, then the code behind for that page creates the view model using Dependency Injection (App.Host.Services.GetService(typeof(T)) as T;
where Host is a static property defined in the app). However, when the frame is defined in a library it doesn't have access to the Host.
The following works, but it's horrendous. Is there a better method?
var apptype = System.Reflection.Assembly.GetEntryAssembly().GetType("SWFieldData.App");
PropertyInfo propertyInfo = apptype.GetProperty("_host", BindingFlags.NonPublic | BindingFlags.Static);
var _host = (Microsoft.Extensions.Hosting.IHost)propertyInfo.GetValue(null, null);
ViewModel = _host.Services.GetService(typeof(SetupDataViewModel)) as SetupDataViewModel;