ServiceLocator.Current - logic with current activated window not working
I have been using windows-app-sdk and winui3 for developing my application which supports multiple windows. As an architecture, I am using ServiceLocator and DI to get the singleton and scoped services for my multiple windows. The logic of ServiceLocator.Current is as follows,
static public ServiceLocator Current
{
get
{
int currentViewId = WindowHelper.CurrentWindowId; // it will get the window id of the current activated window
return _serviceLocators.GetOrAdd(currentViewId, key => new ServiceLocator());
}
}
In a specific case, I have a window say 'A', which has a button 'Save & Print'. When the user clicks this button, then I will save the data and open a print preview in a new window say 'B' and it will be activated as current window. When window 'B' becomes activated window, I still need to perform some post save action in my window 'A'. During those post save actions, when fetching the scoped service from the service locator, it returns the scoped service of window 'B' as it is the current window not the service of window 'A'.
Hence I need a solution to fetch the scoped service from service locator, which should return the service not based on the activated window, but based on the requested Thread (it can be any window whether currently activated or background).
Thanks in advance.