ServiceLocator.Current - logic with current activated window not working

guess_me_if_u_can 126 Reputation points
2022-09-18T17:19:50.013+00:00

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.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
785 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.